-5

Is there any way in java, to create a method, which is expecting two different varargs? I know, with the same object kind it isn't possible because the compiler doesn't know where to start or to end. But why it also isn't possible with to different Object types? For example:

private void ask(int... x, String... b) { }

Please Explain !

snikit
  • 11
  • 4

2 Answers2

0

Java doesnot support multiple varargs. Rules for declaring varargs.

  • Var-arg type When you declare a var-arg parameter, you must specify the type of the argument(s) this parameter of your method can receive. (This can be a primitive type or an object type.)
  • Basic syntax To declare a method using a var-arg parameter, you follow the type with an ellipsis (...), a space, and then the name of the array that will hold the parameters received.
  • Other parameters It's legal to have other parameters in a method that uses a var-arg.
  • Var-args limits The var-arg must be the last parameter in the method's signature, and you can have only one var-arg in a method.

But you still archive it by using collection classes or interface.

chaitanya dalvi
  • 1,539
  • 2
  • 17
  • 25
0

In Java, more then one varargs arguments are not allowed in method parameters. only one varargs argument is allowed,that must be the last parameter of the signature of method.

But you can achieve that by passing two arrays (in your case int[] x, String[] b).

private void ask(int[] x, String[] b) { }