Create a copy of a 2-dimensional array in Java
This post will discuss how to create a copy of a 2-dimensional array in Java with dynamic column size.
1. Using clone() method
A simple solution is to use the clone() method to clone a 2-dimensional array in Java. The following solution uses a for loop to iterate over each row of the original array and then calls the clone() method to copy each row.
|
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 |
import java.util.Arrays; public class Main { public static int[][] copy(int[][] src) { if (src == null) { return null; } int[][] copy = new int[src.length][]; for (int i = 0; i < src.length; i++) { copy[i] = src[i].clone(); } return copy; } public static void main(String[] args) { int[][] src = new int[][] { {1, 2, 3, 4, 5}, {6, 7, 8, 9}, {10, 11, 12} }; int[][] copy = copy(src); System.out.println(Arrays.deepToString(copy)); } } |
Output:
[[1, 2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12]]
The code can be further shortned to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Arrays; public class Main { public static int[][] copy(int[][] src) { if (src == null) { return null; } return Arrays.stream(src).map(int[]::clone).toArray(int[][]::new); } public static void main(String[] args) { int[][] src = new int[][] { {1, 2, 3, 4, 5}, {6, 7, 8, 9}, {10, 11, 12} }; int[][] copy = copy(src); System.out.println(Arrays.deepToString(copy)); } } |
Output:
[[1, 2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12]]
2. Using System.arraycopy() method
You can also use the System.arraycopy() method to copy an array from the specified source array, beginning at the specified position, to the specified position of the destination array. This is demonstrated below:
|
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 |
import java.util.Arrays; public class Main { public static int[][] copy(int[][] src) { if (src == null) { return null; } int[][] copy = new int[src.length][]; for (int i = 0; i < src.length; i++) { copy[i] = new int[src[i].length]; System.arraycopy(src[i], 0, copy[i], 0, src[i].length); } return copy; } public static void main(String[] args) { int[][] src = new int[][] { {1, 2, 3, 4, 5}, {6, 7, 8, 9}, {10, 11, 12} }; int[][] copy = copy(src); System.out.println(Arrays.deepToString(copy)); } } |
Output:
[[1, 2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12]]
3. Using Arrays.copyOf() method
Another alternative is to use the Arrays.copyOf() method to create a complete copy of an array. The following solution uses a for loop to iterate over each row of the original array and then calls the Arrays.copyOf() method to copy each row.
|
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 |
import java.util.Arrays; public class Main { public static int[][] copy(int[][] src) { if (src == null) { return null; } int[][] copy = new int[src.length][]; for (int i = 0; i < src.length; i++) { copy[i] = Arrays.copyOf(src[i], src[i].length); } return copy; } public static void main(String[] args) { int[][] src = new int[][] { {1, 2, 3, 4, 5}, {6, 7, 8, 9}, {10, 11, 12} }; int[][] copy = copy(src); System.out.println(Arrays.deepToString(copy)); } } |
Output:
[[1, 2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12]]
4. Naive solution
You can write your own routine to copy the 2-dimensional array using nested loops, as shown below. However, the solution does not work when the column size isn’t fixed. Note that all the above solutions work even when the column size is not fixed.
|
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 |
import java.util.Arrays; public class Main { public static int[][] copy(int[][] src) { if (src == null) { return null; } if (src.length == 0) { return new int[0][0]; } int[][] copy = new int[src.length][src[0].length]; for (int i = 0; i < src.length; i++) { for (int j = 0; j < src[i].length; j++) { copy[i][j] = src[i][j]; } } return copy; } public static void main(String[] args) { int[][] src = new int[][] { {1, 2, 3, 4, 5}, {6, 7, 8, 9}, {10, 11, 12} }; int[][] copy = copy(src); System.out.println(Arrays.deepToString(copy)); } } |
Output:
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [10, 11, 12, 0, 0]]
That’s all about creating a copy of a 2-dimensional array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)