public static int[] convertListToArray(List<Integer> listResult) {
int[] result = new int[listResult.size()];
int i= 0;
for (int num : listResult) {
result[i++] = num;
}
return result;
}
是否有列表转换为数组没有明确列出迭代的有效途径?也许这可以通过使用类似的方法:
Arrays.copyOf(int [] origin , int newLength );
System.arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
我知道,有记载的溶液在这里。但是,我特别感兴趣的一种有效的方式转换的List<Integer>
到int[]
鉴于从需要转换Integer
到int
,我不认为你会找到的东西比你有什么更有效的,如果我假设你在谈论执行效率。
您可能会发现转化为Integer[]
第一,然后循环可能更有效(下同),但你可能没有了。你不得不测试它在您的具体方案和观望。
下面是一个例子:
int size = listResult.size();
int[] result = new int[size];
Integer[] temp = listResult.toArray(new Integer[size]);
for (int n = 0; n < size; ++n) {
result[n] = temp[n];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句