How can I deep copy an irregularly shaped 2D array in Java?
Ie.
int[][] nums = {{5}, {9,4}, {1,7,8}, {8,3,2,10}}
I'm unable to use Arrays.arrayCopy() for some reason (versioning?)
Arrays.arrayCopy()
int[][] copy = new int[nums.length][]; for (int i = 0; i < nums.length; i++) { copy[i] = new int[nums[i].length]; for (int j = 0; j < nums[i].length; j++) { copy[i][j] = nums[i][j]; } }
You can replace the second loop with System.arraycopy() or Arrays.copyOf().
1.4m articles
1.4m replys
5 comments
57.0k users