7
7
import java .io .IOException ;
8
8
import java .io .ObjectInputStream ;
9
9
import java .io .ObjectOutputStream ;
10
+ import java .util .Arrays ;
11
+ import modern .challenge .shallow .copy .matrix .Matrices ;
10
12
import org .apache .commons .lang3 .SerializationUtils ;
11
13
12
14
public class Main {
13
15
14
16
public static void main (String [] args ) throws CloneNotSupportedException {
15
17
16
18
// shallow copy via manual copy
17
- System .out .println ("Shallow copy via manual copy:" );
19
+ System .out .println ("\n Shallow copy via manual copy:" );
18
20
modern .challenge .shallow .copy .manually .Point point1
19
21
= new modern .challenge .shallow .copy .manually .Point (5 , 5 );
20
22
@@ -159,6 +161,44 @@ public static void main(String[] args) throws CloneNotSupportedException {
159
161
System .out .println ("Clone 7, (x, y, Radius start, Radius end): "
160
162
+ clone7 .getX () + ", " + clone7 .getY ()
161
163
+ ", " + 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 ("\n Shallow 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 ("\n Shallow 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 ("\n Shallow 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 ("\n Shallow 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 );
162
202
}
163
203
164
204
@ SuppressWarnings ("unchecked" )
@@ -182,4 +222,14 @@ private static <T> T cloneThroughSerialization(T t) {
182
222
return t ;
183
223
}
184
224
}
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
+ }
185
235
}
0 commit comments