[英]How can I convert Collection<char[]> to char[][] in java?
I'm trying to speed up an existing implementation of an algorithm in order to parallelize it on the GPU, but to do that all I need to convert all datatypes to primitives.我正在尝试加快算法的现有实现,以便在 GPU 上并行化它,但要做到这一点,我需要将所有数据类型转换为原语。
I have a collection of char-arrays Collection<char[]>
how can I transform it into an array of char-arrays char[][]
?我有一个 char-arrays Collection<char[]>
的集合,我怎样才能将它转换成一个 char-arrays char[][]
的数组?
I tried final char[][] kernelFeatures = (char[][]) features.toArray();
我尝试final char[][] kernelFeatures = (char[][]) features.toArray();
However, I am getting an error message java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [[C ([Ljava.lang.Object; and [[C are in module java.base of loader 'bootstrap')
但是,我收到一条错误消息java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [[C ([Ljava.lang.Object; and [[C are in module java.base of loader 'bootstrap')
java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [[C ([Ljava.lang.Object; and [[C are in module java.base of loader 'bootstrap')
. java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [[C ([Ljava.lang.Object; and [[C are in module java.base of loader 'bootstrap')
The reason you can't use Collection#toArray()
and cast the result to char[][]
is because:您不能使用Collection#toArray()
并将结果转换为char[][]
的原因是:
The returned array's runtime component type is
Object
.返回数组的运行时组件类型为Object
。
You can use Collection#toArray(T[])
which allows to to specify the component type of the array.您可以使用Collection#toArray(T[])
来指定数组的组件类型。
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.要理解为什么不需要指定第二维,请记住 Java 中的二维 arrays 只是一维 arrays ,其中每个元素都是一个数组。 See these Q&As for more information:有关更多信息,请参阅这些问答:
Regarding the use of 0
as the first dimension, note that using 0
is not required;关于使用0
作为第一维,注意使用0
不是必须的; you can use any valid length you want (eg collection.size()
).你可以使用任何你想要的有效长度(例如collection.size()
)。 The reason for using 0
, however, is that it's supposedly more efficient, at least when using an ArrayList
on HotSpot 8:然而,使用0
的原因是它被认为更有效,至少在 HotSpot 8 上使用ArrayList
时:
I'm not sure if it's more efficient in other versions, other JVM implementations, or for other Collection
implementations.我不确定它在其他版本、其他 JVM 实现或其他Collection
实现中是否更有效。
If you're using Java 11+ then there's an alternative solution: Collection#toArray(IntFunction)
.如果您使用的是 Java 11+,那么还有另一种解决方案: Collection#toArray(IntFunction)
。
Collection<char[]> collection = ...;
char[][] array = collection.toArray(char[][]::new);
Which, as a lambda expression, would look like:其中,作为 lambda 表达式,看起来像:
Collection<char[] collection = ...;
char[][] array = collection.toArray(i -> new char[i][]);
Both of the #toArray
methods ultimately do something similar to:这两种#toArray
方法最终都会做类似的事情:
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.尽管您应该使用#toArray
方法之一,因为这允许Collection
实现使用它可以使用的任何优化。 For example, the ArrayList
class uses the System#arraycopy
method.例如, ArrayList
class 使用System#arraycopy
方法。
Try this:尝试这个:
List<char[]> list = List.of(new char[] { '1', '2'
}, new char[] { '3', '4'
}, new char[] { '6', '7'
});
char[][] array = list.stream().toArray(char[][]::new);
System.out.println(Arrays.deepToString(array));
or to print out with a for loop或使用 for 循环打印出来
for (char[] c : array) {
System.out.println(Arrays.toString(c));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.