How to Clone a 2D Array With Different Row Sizes in Java?
Last Updated :
08 Feb, 2024
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 different row sizes in Java.
Methods to Clone a 2D Array with Different Row Sizes
There are various methods to clone 2D arrays with different row sizes. We will see some of them here:
- Using Nested Loops
- Using “System.arraycopy”
- Using Java Streams
Now let’s discuss these methods one by one.
1. Using Nested Loops
We can clone 2D arrays having different row sizes by manually iterating through each element and then copying them one by one. It involves using nested loops to traverse both rows and columns in an array.
Code example:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int [][] realArray
= { { 1 , 2 , 3 , 15 }, { 14 , 5 }, { 6 , 17 , 8 , 9 , 26 } };
int [][] clonedArray = cloneArray(realArray);
displayArray(realArray, "Real Array" );
displayArray(clonedArray, "Cloned Array" );
}
private static int [][] cloneArray( int [][] original)
{
int [][] clone = new int [original.length][];
for ( int i = 0 ; i < original.length; i++) {
clone[i] = new int [original[i].length];
for ( int j = 0 ; j < original[i].length; j++) {
clone[i][j] = original[i][j];
}
}
return clone;
}
private static void displayArray( int [][] array,
String message)
{
System.out.println(message);
for ( int [] row : array) {
for ( int element : row) {
System.out.print(element + " " );
}
System.out.println();
}
System.out.println();
}
}
|
Output:
Real Array
1 2 3 15
14 5
6 17 8 9 26
Cloned Array
1 2 3 15
14 5
6 17 8 9 26
2. Using “System.arraycopy”
The “System.arraycopy” method in Java is used to copy elements from one array to another array. It considers iterating through whole array using “System.arraycopy” and then copy each row using this method.
Code example:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int [][] realArray
= { { 1 , 2 , 3 , 15 }, { 14 , 5 }, { 6 , 17 , 8 , 9 , 26 } };
int [][] clonedArray = cloneArray(realArray);
displayArray(realArray, "Real Array" );
displayArray(clonedArray, "Cloned Array" );
}
private static int [][] cloneArray( int [][] original)
{
int [][] clone = new int [original.length][];
for ( int i = 0 ; i < original.length; i++) {
clone[i] = new int [original[i].length];
System.arraycopy(original[i], 0 , clone[i], 0 , original[i].length);
}
return clone;
}
private static void displayArray( int [][] array,
String message)
{
System.out.println(message);
for ( int [] row : array) {
for ( int element : row) {
System.out.print(element + " " );
}
System.out.println();
}
System.out.println();
}
}
|
Output:
Real Array
1 2 3 15
14 5
6 17 8 9 26
Cloned Array
1 2 3 15
14 5
6 17 8 9 26
3. Using Java Streams
In Java 8, Streams provides functional approach to clone a 2D array with different row sizes.
Code example:
Java
import java.util.Arrays;
import java.util.stream.Collectors;
import java.io.*;
class GFG {
public static void main(String[] args)
{
int [][] realArray
= { { 1 , 2 , 3 , 15 }, { 14 , 5 }, { 6 , 17 , 8 , 9 , 26 } };
int [][] clonedArray = cloneArray(realArray);
displayArray(realArray, "Real Array" );
displayArray(clonedArray, "Cloned Array" );
}
private static int [][] cloneArray( int [][] original) {
return Arrays.stream(original)
.map( int []::clone)
.toArray( int [][]:: new );
}
private static void displayArray( int [][] array,
String message)
{
System.out.println(message);
for ( int [] row : array) {
for ( int element : row) {
System.out.print(element + " " );
}
System.out.println();
}
System.out.println();
}
}
|
Output:
Real Array
1 2 3 15
14 5
6 17 8 9 26
Cloned Array
1 2 3 15
14 5
6 17 8 9 26
Share your thoughts in the comments
Please Login to comment...