Create a method that accept varargs in Java : Varargs « Language Basics « Java






Create a method that accept varargs in Java

  

public class Main {
  public static void main(String[] args) {
    printParams(1, 2, 3);
    printParams(10, 20, 30, 40, 50);
    printParams(100, 200, 300, 400, 500);
  }

  public static void printParams(int... numbers) {
    for (int number : numbers) {
      System.out.println(number + ", ");
    }

  }
}

   
    
  








Related examples in the same category

1.VarArgs Example
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