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;
}
Run Code Online (Sandbox Code Playgroud)
有没有一种有效的方法将List转换为数组而不显式迭代List?也许可以通过使用以下方法:
Arrays.copyOf(int [] origin , int newLength );
System.arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
Run Code Online (Sandbox Code Playgroud)
我知道,有记载的溶液在这里.但是,我特别感兴趣的是一种有效的转换List<Integer>方式int[]