Reputation: 1121
My question is very close to this question. But it's not the same. I have a method that accepts varargs with a signature like
static void doSomething(byte[]... values)
And a List of byte[] that I want to send to that method.
List<byte[]> myList;
How do I convert myList
to byte[]
varargs to send to doSomething
?
I thought it would be something like
doSomething(myList.toArray(new Byte[][0]));
but that did not work - It says unexpected token at 0]))
.
Thanks in advance.
Upvotes: 0
Views: 596
Reputation: 1500675
There are two problems here:
Byte
and byte
are different typesnew byte[0][]
. Arrays of arrays are annoying in that respect, to be honest. I can certainly understand why you'd expect to put the [0]
at the end, but in this case it's just not that way...With that change in place, it's fine:
import java.util.*;
public class Test {
public static void main(String[] args) {
List<byte[]> myList = new ArrayList<>();
myList.add(new byte[10]);
myList.add(new byte[5]);
doSomething(myList.toArray(new byte[0][]));
}
static void doSomething(byte[]... values) {
System.out.printf("Array contained %d values%n", values.length);
}
}
Upvotes: 4