【发布时间】:2021-04-03 18:27:38
【问题描述】:
例子:
public class Example {
public int doMath(String name, int... a) {
System.out.println("My name is " + name);
for (int i: a)
int b = a+a
return (b);
}
public static void main(String args[]){
int[] testArray = new int[]{1,2,3}; //In this case size of the array is 3.
// Could be any number during runtime.
doMath(test[0],test[1],test[2]); //Need to insert values from into the doMath argument,
// dynamically because
// will not know length of array
// during compile time
}
}
我有包含对象的列表/数组,并且我将插入这些值的方法包含一个可变参数。我需要动态地将这些值插入到这个方法中,而不需要对列表的大小进行硬编码。因为在运行时,用户输入可以在 0 到 100 之间变化。而且我需要能够将此列表中的值插入到此方法中,插入到它们的正确位置。
【问题讨论】: