Is it possible to specify in the method, with an indefinite number of arguments, a set of arguments at once, for example an array?

As far as I know, the ability to set an indefinite number of arguments to a method was implemented in order not to set all the arguments as an array. Does backward compatibility remain?

  • If I understand you correctly, you can overload the method. Create a variant with a variable number of parameters, and with any other: test(Type... args){} test(Type param1, Type param2){} - XelaNimed
  • 2
    Backward compatibility with what? - Alexey Shimansky

1 answer 1

I understood your question like this: "Having a method with a variable number of arguments, is it possible to pass arguments to it without listing them one at a time at the calling point, but specifying only one argument — an array containing all the values?"

The answer is yes, you can. If we have a method

 void count(String... strings) { System.out.println(strings.length); } 

Then the following two calls are equivalent:

 count("q", "w", "e"); // напечатает "3" String[] args = {"q", "w", "e"}; count(args); // тоже напечатает "3"