Code: Select all
/**
* Java Program which tries to implement variable argument method using
* method overloading. This started get clumsy once number of parameter exceeds
* five.
*/
class VarargsExample{
/*
* @return multiplication of all numbers in array
*/
public int multiply(int[] numbers){
int result = 1;
for(int number: numbers){
result= result*number;
}
return result;
}
}
public class VarargsExampleTest
{
public static void main (String... args)
{
int[] a = new int[] {1,3,2,4,5};
VarargsExample v = new VarargsExample();
int[] results = new int[5];
for (int i = 0; i < results.length; i++)
{results[i] = v.multiply[a];}
for (int i = 0; i < results.length; i++)
{System.out.println(results[i]);}
}
}
/*
Read more: http://javarevisited.blogspot.com/2011/09/variable-argument-in-java5-varargs.html#ixzz23yTFsnIG
*/