数组果然会很坑。。
在Ints.asList()中
public static List<Integer> asList(int... backingArray) {
return (List)(backingArray.length == 0 ? Collections.emptyList() : new Ints.IntArrayAsList(backingArray));
}
因为数组在内存模型中是new的时候在堆开辟空间 之后栈中指向它的都是指向同一内存地址
所以如下得到的遍历结果一样:
int[] a = {1,2,3};
List<Integer> integers = Ints.asList(a);
a[0] = 10;
List<Integer> integers1 = Ints.asList(a);
integers.forEach(vo-> System.out.println(vo));
integers1.forEach(vo-> System.out.println(vo));
想要不一样只能不涉及nums的地址指向 将其的值分别赋给新的List 然后添加
不然你得到result的会是全一样的
List<Integer> temp = new ArrayList<>();
for(int i:Ints.asList(nums)){
temp.add(i);
}
result.add(temp);
可以不用上面的方法 如果一定要将数组转为list 可以弄一个临时数组temp[] 使用System.arraycopy(src,srcIndex,des,desIndex,length)将nums拷贝到新建的临时数组temp里 然后Int.asList(temp)