Java Program example to demonstrate variable length of arguments
Program
public class VarArgList
{
void varTest(int ... arg)
{
System.out.println("Number of arguments:\t" + arg.length);
System.out.println("Arguments are:");
int i = 1;
for(int x : arg)
{
System.out.println("arg[" + i + "] : " + x);
i++;
}
System.out.println();
}
public static void main(String[] args)
{
VarArgList argLst = new VarArgList();
argLst.varTest(100);
argLst.varTest(10, 20, 30);
argLst.varTest();
}
}
Output 1
Number of arguments: 1
Arguments are:
arg[1] : 100
Output 2
Number of arguments: 3
Arguments are:
arg[1] : 10
arg[2] : 20
arg[3] : 30
Output 3
Number of arguments: 0
Arguments are: