-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwallowDeepCopy.java
More file actions
55 lines (43 loc) · 1.39 KB
/
SwallowDeepCopy.java
File metadata and controls
55 lines (43 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package gr.aueb.cf.ch10.projects;
public class SwallowDeepCopy {
public static void main(String[] args) {
int[][] arr = {{12, 25, 4}, {1, 5, 6}};
// Creates a swallow copy of the arr
int[][] swallowCopy = swallowCopy(arr);
// Creates a deep copy of the arr
int[][] deepCopy = deepCopy(arr);
// Modify an element of the original array
arr[0][0] = 5;
// Prints the original arr
print2DArray(arr);
System.out.println();
// Prints a deep copy
print2DArray(swallowCopy);
System.out.println();
// Prints a deep copy
print2DArray(deepCopy);
}
// Swallow copy method
public static int[][] swallowCopy(int[][] arr) {
return arr.clone();
}
// Deep copy method
public static int[][] deepCopy(int[][] arr) {
int[][] copy = new int[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
copy[i][j] = arr[i][j];
}
}
return copy;
}
// Method to print a 2D array
public static void print2DArray(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}