Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int[][] nums = {{5},
- {9,4},
- {1,7,8},
- {8,3,2,10}}
- 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];
- }
- }
- private static int[][] copy2d(int[][] nums) {
- int[][] copy = new int[nums.length][];
- for (int i = 0; i < copy.length; i++) {
- int[] member = new int[nums[i].length];
- System.arraycopy(nums[i], 0, member, 0, nums[i].length);
- copy[i] = member;
- }
- return copy;
- }
- public class ArrayTest extends TestCase {
- public void testArrays() {
- Object arr = new int[][]{
- {5},
- {9, 4},
- {1, 7, 8},
- {8, 3, 2, 10}
- };
- Object arrCopy = copyNd(arr);
- int height = Array.getLength(arr);
- for (int r = 0; r < height; r++) {
- Object rowOrigonal = Array.get(arr, r);
- Object rowCopy = Array.get(arrCopy, r);
- int width = Array.getLength(rowOrigonal);
- for (int c = 0; c < width; c++) {
- assertTrue(rowOrigonal.getClass().isArray());
- assertTrue(rowCopy.getClass().isArray());
- assertEquals(Array.get(rowOrigonal, c), Array.get(rowCopy, c));
- System.out.println(Array.get(rowOrigonal, c) + ":" + Array.get(rowCopy, c));
- }
- }
- }
- public static Object copyNd(Object arr) {
- if (arr.getClass().isArray()) {
- int innerArrayLength = Array.getLength(arr);
- Class component = arr.getClass().getComponentType();
- Object newInnerArray = Array.newInstance(component, innerArrayLength);
- //copy each elem of the array
- for (int i = 0; i < innerArrayLength; i++) {
- Object elem = copyNd(Array.get(arr, i));
- Array.set(newInnerArray, i, elem);
- }
- return newInnerArray;
- } else {
- return arr;//cant deep copy an opac object??
- }
- }
- }
- public void testMultiDimArray()
- {
- int[][][] arr = new int[][][] {
- { {5}, {5, 6 }, {3, 3, 1} },
- { {1, 2, 3}, {4, 5 } }
- };
- Object[] dest = (Object[]) deepCopy(arr);
- // System.out.println(Arrays.deepToString(dest));
- assertTrue(Arrays.deepEquals(arr, dest));
- }
- public static Object deepCopy(Object src)
- {
- int srcLength = Array.getLength(src);
- Class srcComponentType = src.getClass().getComponentType();
- Object dest = Array.newInstance(srcComponentType, srcLength);
- if (srcComponentType.isArray())
- {
- for (int i = 0; i < Array.getLength(src); i++)
- Array.set(dest, i, deepCopy(Array.get(src, i)));
- }
- else
- {
- System.arraycopy(src, 0, dest, 0, srcLength);
- }
- return dest;
- }
- public static char[][] cloneArray(char[][] array){
- char[][] copy = new char[array.length][];
- for(int i = 0 ; i < array.length ; i++){
- System.arraycopy(array[i], 0, copy[i] = new char[array[i].length], 0, array[i].length);
- }
- return copy;
- }
- import java.util.*;
- public class ArrayDeepCopy {
- static int[][] clone(int[][] arr) {
- final int L = arr.length;
- int[][] clone = new int[L][];
- for (int i = 0; i < clone.length; i++) {
- clone[i] = (arr[i] == null) ? null : arr[i].clone();
- }
- return clone;
- }
- public static void main(String[] args) {
- int[][] a = {
- { 1, },
- { 2, 3, },
- null,
- };
- int[][] b = a.clone();
- System.out.println(a[0] == b[0]); // "true", meaning shallow as expected!
- b = clone(a); // this is deep clone!
- System.out.println(Arrays.deepEquals(a, b)); // "true"
- System.out.println(a[0] == b[0]); // "false", no longer shallow!
- }
- }
- //
- // Sort the points array in descending order.
- // [94, 70, 64, 53, 44]
- //
- Arrays.sort(points, Collections.reverseOrder());
- System.out.println(Arrays.toString(points));
Add Comment
Please, Sign In to add comment