Dave
Dave

Reputation: 769

Java methods combine a list of arrays: variable number of arguments

A method to combine a list of arrays: variable number of arrays. Method signature

public static <T> T[] combine(T[] ... a) {
    ...
} 

byte[] a = [];
byte[] b = [];
byte[] c = [];
combine(a, b, c);   // compiling error

What is the right way to define the method signature for variable number of arrays. Thanks.

Upvotes: 0

Views: 115

Answers (1)

f1sh
f1sh

Reputation: 11934

That's because you cannot substitute primitive types with T.

Try using the wrapper class Byte:

public static void main(String[] args) {
  Byte[] a = new Byte[]{0x0};
  Byte[] b = a;
  Byte[] c = a;
  combine(a, b, c); 
}
public static <T> T[] combine(T[] ... a) {
  //do your magic here
} 

Of course this code does not combine the arrays, but the method call compiles.

Upvotes: 3

Related Questions