77import java .io .IOException ;
88import java .io .ObjectInputStream ;
99import java .io .ObjectOutputStream ;
10+ import java .util .Arrays ;
11+ import modern .challenge .shallow .copy .matrix .Matrices ;
1012import org .apache .commons .lang3 .SerializationUtils ;
1113
1214public 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 ("\n Shallow 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 ("\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 = 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 );
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}
0 commit comments