Java Examples - Using Varargs in Overloaded Methods
The following example demonstrates how to use variable arguments in overloaded methods:
Main.java file
public class Main {
static void vaTest(int ... no) {
System.out.print("vaTest(int ...): "
+ "Number of arguments:" + no.length +"Content:");
for(int n : no)
System.out.print(n + " ");
System.out.println();
}
static void vaTest(boolean ... bl) {
System.out.print("vaTest(boolean ...) " +
"Number of arguments:" + bl.length + "Content:");
for(boolean b : bl)
System.out.print(b + " ");
System.out.println();
}
static void vaTest(String msg, int ... no) {
System.out.print("vaTest(String, int ...): " +
msg +"Number of arguments:"+ no.length +"Content:");
for(int n : no)
System.out.print(n + " ");
System.out.println();
}
public static void main(String args[]){
vaTest(1, 2, 3);
vaTest("Test:", 10, 20);
vaTest(true, false, false);
}
}
The output of running the above code is:
vaTest(int ...): 参数个数: 3 内容: 1 2 3 vaTest(String, int ...): 测试: 参数个数: 2 内容: 10 20 vaTest(boolean ...) 参数个数: 3 内容: true false falseOther Extensions
Java Examples