Problem
The reason you can't use Collection#toArray()
and cast the result to char[][]
is because:
The returned array's runtime component type is Object
.
Solutions
Pre Java 11
You can use Collection#toArray(T[])
which allows to to specify the component type of the array.
Collection<char[]> collection = ...;
char[][] array = collection.toArray(new char[0][]);
To understand why you don't need to specify the second dimension, remember that two-dimensional arrays in Java are simply one-dimensional arrays where each element is an array. See these Q&As for more information:
Regarding the use of 0
as the first dimension, note that using 0
is not required; you can use any valid length you want (e.g. collection.size()
). The reason for using 0
, however, is that it's supposedly more efficient, at least when using an ArrayList
on HotSpot 8:
I'm not sure if it's more efficient in other versions, other JVM implementations, or for other Collection
implementations.
Java 11+
If you're using Java 11+ then there's an alternative solution: Collection#toArray(IntFunction)
.
Collection<char[]> collection = ...;
char[][] array = collection.toArray(char[][]::new);
Which, as a lambda expression, would look like:
Collection<char[] collection = ...;
char[][] array = collection.toArray(i -> new char[i][]);
Manual Copy
Both of the #toArray
methods ultimately do something similar to:
Collection<char[]> collection = ...;
char[][] array = new char[collection.size()][];
int index = 0;
for (char[] element : collection) {
array[index++] = element;
}
Though you should use one of the #toArray
methods because that allows the Collection
implementation to use any optimizations it can. For example, the ArrayList
class uses the System#arraycopy
method.