how to make copy of array instead of reference in java? [duplicate]

随声附和 提交于 2019-12-06 03:20:15

You have to copy each row of the array; you can't copy the array as a whole. You may have heard this called deep copying.

Accept that you will need an honest-to-goodness for loop.

int[][] b = new int[3][];
for (int i = 0; i < 3; i++) {
  b[i] = Arrays.copyOf(a[i], a[i].length);
}

System.arraycopy() should work for you, but it doesn't copy as a whole, it copies "from a specified position to a specified position," according to the java documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!