Java 5 varargs feature allows you to pass a variable number of arguments to methods. Groovy also support this feature, See the below example.
Java way for implementing varargs.
public class VarargsDemo { public static void main(String[] args) { int[] numbers = {10,20,30,40,50}; System.out.println("Total is " + VarargsDemo.calculateTotal(numbers)); } public static int calculateTotal(int... numbers) { int total = 0; for(int number : numbers) { total += number; } return total; } }
Groovier way for implementing varargs.
def calculateTotal(int... numbers) { def total = 0 for(number in numbers) { total += number } total } println calculateTotal(10,20,30,40,50)
Comments