8000 Objects, immutability and switch expressions · yogiprogram/Java-Coding-Problems@b9dd146 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9dd146

Browse files
committed
Objects, immutability and switch expressions
1 parent 4635954 commit b9dd146

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

Chapter02/P53_CloningObjects/src/main/java/modern/challenge/Main.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
import java.io.IOException;
88
import java.io.ObjectInputStream;
99
import java.io.ObjectOutputStream;
10+
import java.util.Arrays;
11+
import modern.challenge.shallow.copy.matrix.Matrices;
1012
import org.apache.commons.lang3.SerializationUtils;
1113

1214
public class Main {
1315

1416
public static void main(String[] args) throws CloneNotSupportedException {
1517

1618
// shallow copy via manual copy
17-
System.out.println("Shallow copy via manual copy:");
19+
System.out.println("\nShallow copy via manual copy:");
1820
modern.challenge.shallow.copy.manually.Point point1
1921
= new modern.challenge.shallow.copy.manually.Point(5, 5);
2022

@@ -159,6 +161,44 @@ public static void main(String[] args) throws CloneNotSupportedException {
159161
System.out.println("Clone 7, (x, y, Radius start, Radius end): "
160162
+ clone7.getX() + ", " + clone7.getY()
161163
+ ", " + clone7.getRadius().getStart() + ", " + clone7.getRadius().getEnd());
164+
165+
int[][] source = {{3, 1, 5}, {3, 6, 7}};
166+
167+
// shallow copy of matrix (1)
168+
System.out.println("\nShallow copy of matrix via manual copy (1):");
169+
int[][] target1 = Matrices.cloneArrayOfInt1(source);
170+
target1[0][0] = 0;
171+
System.out.println("Original array:");
172+
printMatrix(source);
173+
System.out.println("Cloned and modified array:");
174+
printMatrix(target1);
175+
176+
// shallow copy of matrix (2)
177+
System.out.println("\nShallow copy of matrix via manual copy (2):");
178+
int[][] target2 = 10000 Matrices.cloneArrayOfInt2(source);
179+
target2[0][0] = 0;
180+
System.out.println("Original array:");
181+
printMatrix(source);
182+
System.out.println("Cloned and modified array:");
183+
printMatrix(target2);
184+
185+
// shallow copy of matrix (3)
186+
System.out.println("\nShallow copy of matrix via manual copy (3):");
187+
int[][] target3 = Matrices.cloneArrayOfInt3(source);
188+
target3[0][0] = 0;
189+
System.out.println("Original array:");
190+
printMatrix(source);
191+
System.out.println("Cloned and modified array:");
192+
printMatrix(target3);
193+
194+
// shallow copy of matrix (4)
195+
System.out.println("\nShallow copy of matrix via manual copy (4):");
196+
int[][] target4 = Matrices.cloneArrayOfInt4(source);
197+
target4[0][0] = 0;
198+
System.out.println("Original array:");
199+
printMatrix(source);
200+
System.out.println("Cloned and modified array:");
201+
printMatrix(target4);
162202
}
163203

164204
@SuppressWarnings("unchecked")
@@ -182,4 +222,14 @@ private static <T> T cloneThroughSerialization(T t) {
182222
return t;
183223
}
184224
}
225+
226+
// helper method to display a matrix
227+
private static void printMatrix(int[][] matrix) {
228+
for (int i = 0; i < matrix.length; i++) {
229+
for (int j = 0; j < matrix[0].length; j++) {
230+
System.out.print(matrix[i][j] + " ");
231+
}
232+
System.out.println();
233+
}
234+
}
185235
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package modern.challenge.shallow.copy.matrix;
2+
3+
import java.util.Arrays;
4+
5+
public final class Matrices {
6+
7+
private Matrices() {
8+
throw new AssertionError("Cannot be instantiated");
9+
}
10+
11+
public static int[][] cloneArrayOfInt1(int[][] source) {
12+
13+
if (source == null) {
14+
throw new IllegalArgumentException("The given array cannot be null");
15+
}
16+
17+
int length = source.length;
18+
int[][] target = new int[length][source[0].length];
19+
20+
for (int i = 0; i < length; i++) {
21+
target[i] = source[i].clone();
22+
}
23+
24+
return target;
25+
}
26+
27+
public static int[][] cloneArrayOfInt2(int[][] source) {
28+
29+
if (source == null) {
30+
throw new IllegalArgumentException("The given array cannot be null");
31+
}
32+
33+
int length = source.length;
34+
int[][] target = new int[length][source[0].length];
35+
for (int i = 0; i < length; i++) {
36+
System.arraycopy(source[i], 0, target[i], 0, source[i].length);
37+
}
38+
return target;
39+
}
40+
41+
public static int[][] cloneArrayOfInt3(int[][] source) {
42+
43+
if (source == null) {
44+
throw new IllegalArgumentException("The given array cannot be null");
45+
}
46+
47+
int length = source.length;
48+
int[][] target = new int[length][];
49+
for (int i = 0; i < length; i++) {
50+
target[i] = Arrays.copyOf(source[i], source[i].length);
51+
}
52+
return target;
53+
}
54+
55+
public static int[][] cloneArrayOfInt4(int[][] source) {
56+
return Arrays.stream(source)
57+
.map(int[]::clone)
58+
.toArray(int[][]::new);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)
0