Deep Copy of 2D Array in Java Last Updated : 02 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, performing a deep copy of a 2D array containing objects requires careful consideration to ensure independence between the original and copied arrays. By creating new instances of objects for each element in the array, developers can achieve a true deep copy, preventing unintended side effects due to shared references. In this article, we will learn to create a deep copy of a 2D array in Java. Illustration of Shallow Copy Vs Deep Copy Consider a 2D array 'originalArray' containing objects Original Array : [[obj1, obj2] , [obj3], [obj4]]Copy Type Description Example Shallow Copy Both arrays share references to the same objects Shallow Copied Array : [[obj1, obj2] , [obj3], [obj4]] Deep CopyNew instances of objects are created. Deep copied array : [[newObj1, newObj2] , [newObj3], [newObj4]] Java Program to Create Deep Copy of a 2D Array Below is the implementation of the topic: Java // Java Program for Deep Copy of 2D Array public class DeepCopy2DArray { // A simple class for illustration purposes static class MyClass { int value; MyClass(int value) { this.value = value; } } public static void main(String[] args) { // Create the original 2D array with objects MyClass[][] originalArray = { {new MyClass(1), new MyClass(2)}, {new MyClass(3), new MyClass(4)} }; // Perform the deep copy MyClass[][] deepCopiedArray = deepCopy2DArray(originalArray); // Modify an element in the original array to demonstrate independence originalArray[0][0].value = 99; // Display the original and deep copied arrays System.out.println("Original Array:"); print2DArray(originalArray); System.out.println("\nDeep Copied Array:"); print2DArray(deepCopiedArray); } // Method to perform a deep copy of a 2D array with objects private static MyClass[][] deepCopy2DArray(MyClass[][] originalArray) { int rows = originalArray.length; int cols = originalArray[0].length; MyClass[][] copiedArray = new MyClass[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { // Create a new instance of the object for each element copiedArray[i][j] = new MyClass(originalArray[i][j].value); } } return copiedArray; } // Method to print a 2D array with objects private static void print2DArray(MyClass[][] array) { for (MyClass[] row : array) { for (MyClass element : row) { System.out.print(element.value + " "); } System.out.println(); } } } OutputOriginal Array: 99 2 3 4 Deep Copied Array: 1 2 3 4 Explanation of the above Program:Create the Original 2D Array : We create a 2D array originalArray containing objects of the MyClass class.Perform the Deep Copy : The deepCopy2DArray method is implemented to create a new array and instantiate new objects for each element, ensuring a deep copy.Modify an Element in the Original Array : To demonstrate the independence of the deep copied array, we modify an element in the original array after the deep copy operation.Display the Arrays : We print both the original and deep copied arrays to observe the effect of the modification. Comment More infoAdvertise with us Next Article equals() and deepEquals() Method to Compare two Arrays in Java C chalti Follow Improve Article Tags : Java Java Programs Java-Arrays Java Examples Practice Tags : Java Similar Reads How to Make a Deep Copy of Java ArrayList? The Advantage of ArrayList is it can be defined without giving a predefined size. But the disadvantage is it is more expensive to create and maintain. To have the solution for these expenses we can create a deep copy of an ArrayList. There are two types of copies that can be made the first one is a 3 min read Dynamic Array in Java Arrays are linear data structures, and similar types of elements will be inserted in continuous memory locations. Now as we know there is an issue with arrays that size needs to be specified at the time of declaration or taken from the user in Java. Hence, there arise dynamic arrays in Java in which 3 min read equals() and deepEquals() Method to Compare two Arrays in Java Arrays. equals() method does not compare recursively if an array contains another array on other hand Arrays. deepEquals() method compare recursively if an array contains another array. Arrays.equals(Object[], Object[]) Syntax : public static boolean equals(int[] a, int[] a2) Parameters : a - one ar 3 min read How to Clone a 2D Array With Different Row Sizes in Java? 2D arrays are two-dimensional arrays. These are the simplest forms of multidimensional arrays. Java provides various methods to clone the arrays, but when dealing with 2D arrays having varying sizes, then the process becomes more difficult. Here, we will see different methods to clone 2D arrays with 5 min read Print a 2D Array or Matrix in Java In this article, we will learn to Print 2 Dimensional Matrix. 2D-Matrix or Array is a combination of Multiple 1 Dimensional Arrays. In this article we cover different methods to print 2D Array. When we print each element of the 2D array we have to iterate each element so the minimum time complexity 4 min read Difference between multidimensional array in C++ and Java Prerequisites: Multidimensional array in C++, Multidimensional array in Java Multidimensional Arrays: Multidimensional arrays are a tabular representation of arrays to store multiple elements. These dimensions can be 1D arrays, 2D-arrays, etc. Multidimensional arrays are available in both C++ and Ja 4 min read Like