Old style: Use an array to pass a variable number of arguments : Varargs « Language Basics « Java






Old style: Use an array to pass a variable number of arguments

Old style: Use an array to pass a variable number of arguments
  
public class PassArray {
  static void vaTest(int v[]) {
    System.out.print("Number of args: " + v.length + " Contents: ");

    for (int x : v)
      System.out.print(x + " ");

    System.out.println();
  }

  public static void main(String args[]) {
    int n1[] = { 10 };
    int n2[] = { 1, 2, 3 };
    int n3[] = {};

    vaTest(n1); // 1 arg
    vaTest(n2); // 3 args
    vaTest(n3); // no args
  }
}

           
         
    
  








Related examples in the same category

1.VarArgs Example
2.Create a method that accept varargs in Java
3.Varargs, overloading, and ambiguity.
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