public class TestArguments {
public static void main(String[] args) {
int[] arr = new int[]{1, 2, 3};
foo("kk", 365, new int[]{1, 2, 3}, "pc", "iphone");
}
public TestArguments() {
// TODO Auto-generated constructor stub
}
private static void foo(String a, int b, Object... args) {
Object[] ob = new Object[2 + args.length];
ob[0] = a;
ob[1] = b;
for (int i = 0; i < args.length; i++) {
ob[i + 2] = args[i];
}
// foo2(args, a, b);
foo2(ob);
}
private static void foo2(Object... args) {
for (Object obj : args) {
System.out.println(obj.toString());
}
}
}
java的可變參數列表實際上就是一個數組,項目如果有這樣的需求:函數A 調用了函數B, 函數A有一個可變參數列表 作爲輸入參數,現在我需要在函數A的參數列表的最前面追加幾個額外的參數,然後把這個新的參數列表傳遞給函數B。