java怎么把一个list 转换成可变数量参数传入

在 Java 中,如果你有一个 List<T>,并且需要把它传给一个方法的可变参数(varargs) ,可以通过 toArray 来实现。关键点是:可变参数在底层就是一个数组。

import java.util.List;

import java.util.ArrayList;

public class VarargsExample {

public static void main(String[] args) {

// 假设我们有一个List<String>

List<String> list = new ArrayList<>();

list.add("A");

list.add("B");

list.add("C");

**//调用一个接受可变参数的方法

printStrings(list.toArray(new String[0]));

}

*//*可变参数方法

public static void printStrings(String... strings) {

for (String s : strings) {

System.out.println(s);

}

}

}

关键点说明

  • list.toArray(new String[0]) 会返回一个 String[],这是可变参数的底层类型。
  • 如果你的方法是 children(DslTestPlanChild... children),而你有 List<DslTestPlanChild>,可以这样:

children(list.toArray(new DslTestPlanChild[0]))

总结

  • List → Array → Varargs 是标准做法。
  • new T[0] 是惯用写法,JVM 会自动分配正确大小的数组。
java 复制代码
package com.example.dsl.runner;

import com.example.dsl.model.Step;
import com.example.dsl.curl.CurlParser;
import com.example.dsl.model.ParsedCurl;
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;
import us.abstracta.jmeter.javadsl.core.controllers.DslTransactionController;
import us.abstracta.jmeter.javadsl.core.threadgroups.DslDefaultThreadGroup;
import us.abstracta.jmeter.javadsl.http.*;
import java.nio.file.*;
import java.util.*;

public class CsvStepRunner {

    public static List<Step> loadCsv(String csvPath) {
        List<Step> list = new ArrayList<>();
        try {
            List<String> lines = Files.readAllLines(Paths.get(csvPath));
            lines.remove(0);
            for (String l : lines) {
                String[] p = l.split(",");
                Step s = new Step();
                s.type = p[0];
                s.curl = p[1];
                s.extractPath = p.length>2 ? p[2] : "";
                s.extractVar = p.length>3 ? p[3] : "";
                s.assertPath = p.length>4 ? p[4] : "";
                s.assertExpected = p.length>5 ? p[5] : "";
                list.add(s);
            }
        } catch (Exception e) { throw new RuntimeException(e); }
        return list;
    }

    public static DslDefaultThreadGroup buildPlanFromCsv(String csvPath) {
        List<Step> steps = loadCsv(csvPath);
        List<DslHttpSampler> samplers = new ArrayList<>();

        for (Step s : steps) {
            ParsedCurl pc = CurlParser.parse(s.curl);
            DslHttpSampler sampler = httpSampler(pc.url).method(pc.method);
            pc.headers.forEach(sampler::header);
            if (pc.body != null) sampler.body(pc.body);

            if (s.extractPath != null && !s.extractPath.isEmpty()) {
                sampler.children(jsonExtractor(s.extractVar, s.extractPath));
            }
            if (s.assertPath != null && !s.assertPath.isEmpty()) {
                sampler.children(jsonAssertion(s.assertPath).matches(s.assertExpected));
            }
            samplers.add(sampler);
        }

        System.out.println(samplers.toString().getClass());
        DslTransactionController t = transaction("my tr", samplers.toArray(new DslHttpSampler[0]));

        return  threadGroup(1, 1,  transaction("", t)).children(
                        jsr223PostProcessor(c ->System.out.println(c.prevMap())),
                        jsr223PostProcessor(c ->System.out.println(c.prevRequest())),
                        jsr223PostProcessor(c ->System.out.println(c.prevResponse()))
        );
    }

}
 
相关推荐
kszlgy1 天前
Day 52 神经网络调参指南
python
wrj的博客1 天前
python环境安装
python·学习·环境配置
Pyeako1 天前
深度学习--BP神经网络&梯度下降&损失函数
人工智能·python·深度学习·bp神经网络·损失函数·梯度下降·正则化惩罚
星火开发设计1 天前
C++ 数组:一维数组的定义、遍历与常见操作
java·开发语言·数据结构·c++·学习·数组·知识
TTGGGFF1 天前
控制系统建模仿真(一):掌握控制系统设计的 MAD 流程与 MATLAB 基础运算
开发语言·matlab
2501_944424121 天前
Flutter for OpenHarmony游戏集合App实战之贪吃蛇食物生成
android·开发语言·flutter·游戏·harmonyos
摘星编程1 天前
OpenHarmony环境下React Native:Geolocation地理围栏
python
充值修改昵称1 天前
数据结构基础:从二叉树到多叉树数据结构进阶
数据结构·python·算法
Lhuu(重开版1 天前
JS:正则表达式和作用域
开发语言·javascript·正则表达式
点云SLAM1 天前
C++内存泄漏检测之Windows 专用工具(CRT Debug、Dr.Memory)和Linux 专业工具(ASan 、heaptrack)
linux·c++·windows·asan·dr.memory·c++内存泄漏检测·c++内存管理