VarArgs Example : Varargs « Language Basics « Java






VarArgs Example

  

public class VarArgsExample {
    int sumArrays(int[]... intArrays)
    {
        int sum, i, j;

        sum=0;
        for(i=0; i<intArrays.length; i++) {
            for(j=0; j<intArrays[i].length; j++) {
                sum += intArrays[i][j];
            }
        }

        return(sum);
    }

    public static void main(String args[])
    {
        VarArgsExample va = new VarArgsExample();
        int sum=0;

        sum = va.sumArrays(new int[]{1,2,3},
                           new int[]{4,5,6},
                           new int[]{100,200});
        System.out.println("The sum of the numbers is: " + sum);
    }
}
           
         
    
  








Related examples in the same category

1.Create a method that accept varargs in Java
2.Varargs, overloading, and ambiguity.
3.Old style: Use an array to pass a variable number of argumentsOld style: Use an array to pass a variable number of arguments
4.Demonstrate variable length arguments. Demonstrate variable length arguments.
5.Use varargs with standard arguments. Use varargs with standard arguments.
6.Varargs and overloading.Varargs and overloading.
7.Java Varargs TesterJava Varargs Tester
8.Java enum and varargs
9.Java varargs: Iterating Over Variable Length Argument Lists
10.New parameter for main method
11.Convert varargs to an array