0

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.

2
  • Not sure why you get downvoted. It's a valid question. Commented May 13, 2016 at 15:02
  • Even if the three arrays had non-primitive elements, how would you create a new array of T to combine them into? Commented May 13, 2016 at 15:22

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.