// Simple use of varargs - Page 197 staticintsum(int... args) { intsum=0; for (int arg : args) sum += arg; return sum; }
// The WRONG way to use varargs to pass one or more arguments! - Page 197 // static int min(int... args) { // if (args.length == 0) // throw new IllegalArgumentException("Too few arguments"); // int min = args[0]; // for (int i = 1; i < args.length; i++) // if (args[i] < min) // min = args[i]; // return min; // }
// The right way to use varargs to pass one or more arguments - Page 198 staticintmin(int firstArg, int... remainingArgs) { intmin= firstArg; for (int arg : remainingArgs) if (arg < min) min = arg; return min; }