3

I want to pass two arrays of Strings into one string varsargs.

ie.

public void doSomething(String... ){

}

public void test(){
String[] arrayOne = ...
String[] arrayTwo = ...
doSomething(arrayOne, arrayTwo); //Doesn't work but just for an example
}

Is the best way to just concat the two arrays or is there a better way of doing this?

Neal
  • 137
  • 8
  • 1
    You need to concat both arrays together, and then call the other method. See also [How can I concatenate two arrays in Java?](https://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) – Ferrybig Apr 17 '19 at 09:09

4 Answers4

6

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]);
Lino
  • 19,604
  • 6
  • 47
  • 65
1

This is because a vararg has to be the last parameter in a function. Here is an extract from Oracle documentation:

https://docs.oracle.com/javase/8/docs/technotes/guides/language/varargs.html

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position

String... is replaced by String[], so you can't pass two array in one function expecting a vararg.

You would have to merge your arrays into one.

mate00
  • 2,727
  • 5
  • 26
  • 34
-1

You can only pass multiple array in the varargs if they all are of same type

public static void doSomething(String[] ...args){

}

public static void test(){
String[] arrayOne = {};
String[] arrayTwo = {};
doSomething(arrayOne, arrayTwo); //Doesn't work but just for an example
}
Bishal Jaiswal
  • 1,684
  • 13
  • 15
  • Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Aug 16 '22 at 13:24
-2

You can merge two arrays with this instruction:

import org.apache.commons.lang3;
// ...

String[] both = (String[])ArrayUtils.addAll(arrayOne , arrayTwo);

doSomething(both);
// ...

or you can pass both arrays.

public void doSomething(String[] pArrayOne, String[] pArrayTwo ){

}

public void test(){
  String[] arrayOne = ...
  String[] arrayTwo = ...
  doSomething(arrayOne, arrayTwo); 
}
mate00
  • 2,727
  • 5
  • 26
  • 34
FranAB
  • 96
  • 7
  • 3
    At least mention that `ArrayUtils` is from Apache commons (and maybe that it is taken from [here](https://stackoverflow.com/a/80559/5515060)) – Lino Apr 17 '19 at 09:12