Saturday, May 30, 2015

Variable Args..


// In the method declaration, you specify the type followed by an ellipsis (...) Only one variable -length parameter may
// be specified in a method, and this parameter must be the last parameter.

public class ClassA {

public static void main(String[] args) {

// calling method with variable args
printMax(11,40,2,45);
printMax(new double[]{100,33,1,110});

}

public static void printMax(double... numbers){

if(numbers.length ==0){
System.out.println("No argument passed");
}
double result = numbers[0];
for(int i =1; i < numbers.length; i++){
if(numbers[i]>  result){
result = numbers[i];
}
}
System.out.println("The max value is "+ result);
}
}


No comments:

Post a Comment