code_junkie

How to deep copy an irregular 2D array

Nov 13th, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. int[][] nums = {{5},
  2. {9,4},
  3. {1,7,8},
  4. {8,3,2,10}}
  5.  
  6. int[][] copy = new int[nums.length][];
  7.  
  8. for (int i = 0; i < nums.length; i++) {
  9. copy[i] = new int[nums[i].length];
  10.  
  11. for (int j = 0; j < nums[i].length; j++) {
  12. copy[i][j] = nums[i][j];
  13. }
  14. }
  15.  
  16. private static int[][] copy2d(int[][] nums) {
  17. int[][] copy = new int[nums.length][];
  18.  
  19. for (int i = 0; i < copy.length; i++) {
  20. int[] member = new int[nums[i].length];
  21. System.arraycopy(nums[i], 0, member, 0, nums[i].length);
  22. copy[i] = member;
  23. }
  24.  
  25. return copy;
  26. }
  27.  
  28. public class ArrayTest extends TestCase {
  29.  
  30. public void testArrays() {
  31. Object arr = new int[][]{
  32. {5},
  33. {9, 4},
  34. {1, 7, 8},
  35. {8, 3, 2, 10}
  36. };
  37.  
  38. Object arrCopy = copyNd(arr);
  39. int height = Array.getLength(arr);
  40. for (int r = 0; r < height; r++) {
  41. Object rowOrigonal = Array.get(arr, r);
  42. Object rowCopy = Array.get(arrCopy, r);
  43. int width = Array.getLength(rowOrigonal);
  44. for (int c = 0; c < width; c++) {
  45. assertTrue(rowOrigonal.getClass().isArray());
  46. assertTrue(rowCopy.getClass().isArray());
  47. assertEquals(Array.get(rowOrigonal, c), Array.get(rowCopy, c));
  48. System.out.println(Array.get(rowOrigonal, c) + ":" + Array.get(rowCopy, c));
  49. }
  50. }
  51. }
  52.  
  53. public static Object copyNd(Object arr) {
  54. if (arr.getClass().isArray()) {
  55. int innerArrayLength = Array.getLength(arr);
  56. Class component = arr.getClass().getComponentType();
  57. Object newInnerArray = Array.newInstance(component, innerArrayLength);
  58. //copy each elem of the array
  59. for (int i = 0; i < innerArrayLength; i++) {
  60. Object elem = copyNd(Array.get(arr, i));
  61. Array.set(newInnerArray, i, elem);
  62. }
  63. return newInnerArray;
  64. } else {
  65. return arr;//cant deep copy an opac object??
  66. }
  67. }
  68. }
  69.  
  70. public void testMultiDimArray()
  71. {
  72. int[][][] arr = new int[][][] {
  73. { {5}, {5, 6 }, {3, 3, 1} },
  74. { {1, 2, 3}, {4, 5 } }
  75. };
  76.  
  77. Object[] dest = (Object[]) deepCopy(arr);
  78. // System.out.println(Arrays.deepToString(dest));
  79. assertTrue(Arrays.deepEquals(arr, dest));
  80. }
  81.  
  82. public static Object deepCopy(Object src)
  83. {
  84. int srcLength = Array.getLength(src);
  85. Class srcComponentType = src.getClass().getComponentType();
  86.  
  87. Object dest = Array.newInstance(srcComponentType, srcLength);
  88.  
  89. if (srcComponentType.isArray())
  90. {
  91. for (int i = 0; i < Array.getLength(src); i++)
  92. Array.set(dest, i, deepCopy(Array.get(src, i)));
  93. }
  94. else
  95. {
  96. System.arraycopy(src, 0, dest, 0, srcLength);
  97. }
  98.  
  99. return dest;
  100. }
  101.  
  102. public static char[][] cloneArray(char[][] array){
  103. char[][] copy = new char[array.length][];
  104. for(int i = 0 ; i < array.length ; i++){
  105. System.arraycopy(array[i], 0, copy[i] = new char[array[i].length], 0, array[i].length);
  106. }
  107. return copy;
  108. }
  109.  
  110. import java.util.*;
  111.  
  112. public class ArrayDeepCopy {
  113.  
  114. static int[][] clone(int[][] arr) {
  115. final int L = arr.length;
  116. int[][] clone = new int[L][];
  117. for (int i = 0; i < clone.length; i++) {
  118. clone[i] = (arr[i] == null) ? null : arr[i].clone();
  119. }
  120. return clone;
  121. }
  122.  
  123. public static void main(String[] args) {
  124. int[][] a = {
  125. { 1, },
  126. { 2, 3, },
  127. null,
  128. };
  129. int[][] b = a.clone();
  130. System.out.println(a[0] == b[0]); // "true", meaning shallow as expected!
  131.  
  132. b = clone(a); // this is deep clone!
  133. System.out.println(Arrays.deepEquals(a, b)); // "true"
  134. System.out.println(a[0] == b[0]); // "false", no longer shallow!
  135. }
  136. }
  137.  
  138. //
  139. // Sort the points array in descending order.
  140. // [94, 70, 64, 53, 44]
  141. //
  142. Arrays.sort(points, Collections.reverseOrder());
  143. System.out.println(Arrays.toString(points));
Add Comment
Please, Sign In to add comment