Java Two Varargs in One Method

Java two varargs in one method

Only one vararg, sorry. But using asList() makes it almost as convenient:

 public void myMethod(List<Integer> args1, List<Integer> args2) {
...
}

-----------

import static java.util.Arrays.asList;
myMethod(asList(1,2,3), asList(4,5,6));

More than one varargs argument

This is not possible because there is no way the second argument is determined.

To understand why it should be the last argument,
Consider your method

public void myMethod(Foo... f, Foo f1){} 

Now suppose you call it using myMethod(fooObj1, fooObj2, fooObj3);

All the foo arguments will be applied for the var-arg method parameter. Hence there is no way to tell a specific object is passed as the second argument.

Now when you keep the var-arg as the last parameter,

public void meMethod(Foo f1, Foo... f){}

The method call myMethod(fooObj1, fooObj2, fooObj3); will assign fooObj1 to f1 and fooObj2 and fooObj3 will be applied to the var-arg f

To solve this problem, you will have to do

public void myMethod(Foo[] f, Bar... b) {

}

How to pass two arrays into one varargs

Sadly not possible in java as there is no spread operator (like in Kotlin, Ecmascript 6). You have to work your way around this by creating an intermediate array:

String[] arrayThree = new String[arrayOne.length + arrayTwo.length];
System.arraycopy(arrayOne, 0, arrayThree, 0, arrayOne.length);
System.arraycopy(arrayTwo, 0, arrayThree, arrayOne.length, arrayTwo.length);

doSomething(arrayThree);

Or using Streams:

String[] arrayThree = Stream.concat(Arrays.stream(arrayOne), Arrays.stream(arrayTwo))
.toArray(String[]::new);
doSomething(arrayThree);

As said, this is possible in kotlin and can be done like this:

val arrayOne: Array<String> = ...
val arrayTwo: Array<String> = ...

doSomething(*arrayOne, *arrayTwo)

or even in javascript:

const arrayOne = ...
const arrayTwo = ...

doSomething([...arrayOne, ...arrayTwo]);

Builder pattern multiple varargs

No method signature (constructors included) allows for multiple varargs. There can be only one, and it has to be the last argument.

That is just a limitation in the language specification. And yes, the reason for that is likely that it can become ambiguous very fast if you allowed more flexibility.

In the builder pattern, there is no such limitation, as every parameter can get its own method.

  builder
.withOptions("a", "b", "c") // varargs
.withColors("red", "blue") // more varargs
.build();

Pass multiple args to a method with varargs of Object type

You have a non-variadic parameter head.

This means that you require at least head to be passed to the method, with optional variadic arguments after.

Therefore, you should call:

Object[] allArgs = new Object[]{object1, object2, object3};
Object[] varags = Arrays.copyOfRange(allArgs, 1, allArgs.length);
process(allArgs[0], varags)

Multiple object types for varargs in a method prototype?

There is no way in the Java programming language to get it to work so that you can pass an arbitrary number of strings and integers, and have the compiler give an error when you pass something else than a string or integer.

Scala: Why can't a method have multiple vararg arguments?

Scala is based on Java Virtual Machine, which is set to accept varargs only as the last argument, only one per method arguments set. No working this around, this is how the compiler works.

To put it into perspective, imagine a method signature like this:

someMethod(strings1: String*, strings2: String*)

Let's say you pass 4 separate Strings when calling it. The compiler would not know which String object belongs to which vararg.

How to pass an arbitrary number of the same type arguments to a method using varargs?

It's possible to pass an arbitrary number of the same type of arguments to a method using the special syntax named varargs (variable-length arguments). These arguments are specified by three dots after the type. In the body of the method, you can process this parameter as a regular array of the specified type.

Your method takes an integer vararg parameter and outputs the number of arguments in the standard output using the length property of arrays.

... is a special syntax used here to specify a vararg parameter.

i am trying to provide you both types or passing varargs in methods in java:

incorrect example:

public static void method(double... varargs, int a) { /* do something */ }

The correct version of the method is:

public static void method(int a, double... varargs) { /* do something */ }


Related Topics



Leave a reply



Submit