Open In App

How to Clone a 2D Array With Different Row Sizes in Java?

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Using Nested Loops
  2. Using “System.arraycopy”
  3. 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




// Java Program to Clone a 2D Array with
// Different Row Sizes Using Nested Loops  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Define a 2D array with integer values
        int[][] realArray
            = { { 1, 2, 3, 15 }, { 14, 5 }, { 6, 17, 8, 9, 26 } };
          
        // Clone the realArray using cloneArray method
        int[][] clonedArray = cloneArray(realArray);
          
        // Display both the real and cloned arrays
        displayArray(realArray, "Real Array");
        displayArray(clonedArray, "Cloned Array");
    }
  
    // Method to clone a 2D array
    private static int[][] cloneArray(int[][] original)
    {
        // Create a new array with the same dimensions as the original
        int[][] clone = new int[original.length][];
  
        // Iterate over each row of the original array
        for (int i = 0; i < original.length; i++) {
            // Create a new array for each row of the clone
            clone[i] = new int[original[i].length];
              
            // Copy the elements from the original array to the clone
            for (int j = 0; j < original[i].length; j++) {
                clone[i][j] = original[i][j];
            }
        }
        return clone; // Return the cloned array
    }
  
    // Method to display a 2D array
    private static void displayArray(int[][] array,
                                     String message)
    {
        // Print a message indicating the purpose of the array
        System.out.println(message);
          
        // Iterate over each row of the array
        for (int[] row : array) {
            // Iterate over each element in the row and print it
            for (int element : row) {
                System.out.print(element + " ");
            }
            // Move to the next line after printing each row
            System.out.println();
        }
        // Print an empty line for clarity
        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




// Java Program to Clone a 2D Array with
// Different Row Sizes Using "System.arraycopy"   
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Define a 2D array with integer values
        int[][] realArray
            = { { 1, 2, 3, 15 }, { 14, 5 }, { 6, 17, 8, 9, 26 } };
          
        // Clone the realArray using cloneArray method
        int[][] clonedArray = cloneArray(realArray);
          
        // Display both the real and cloned arrays
        displayArray(realArray, "Real Array");
        displayArray(clonedArray, "Cloned Array");
    }
  
    // Method to clone a 2D array
    private static int[][] cloneArray(int[][] original)
    {
        // Create a new array with the same dimensions as the original
        int[][] clone = new int[original.length][];
  
        // Iterate over each row of the original array
        for (int i = 0; i < original.length; i++) {
            // Create a new array for each row of the clone
            clone[i] = new int[original[i].length];
              
            // Use System.arraycopy() to copy the elements from the original array to the clone
            System.arraycopy(original[i], 0, clone[i], 0, original[i].length);
        }
        return clone; // Return the cloned array
    }
  
    // Method to display a 2D array
    private static void displayArray(int[][] array,
                                     String message)
    {
        // Print a message indicating the purpose of the array
        System.out.println(message);
          
        // Iterate over each row of the array
        for (int[] row : array) {
            // Iterate over each element in the row and print it
            for (int element : row) {
                System.out.print(element + " ");
            }
            // Move to the next line after printing each row
            System.out.println();
        }
        // Print an empty line for clarity
        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




// Java Program to Clone a 2D Array with
// Different Row Sizes Using Java Streams 
import java.util.Arrays;
import java.util.stream.Collectors;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Define a 2D array with integer values
        int[][] realArray
            = { { 1, 2, 3, 15 }, { 14, 5 }, { 6, 17, 8, 9, 26 } };
          
        // Clone the realArray using cloneArray method
        int[][] clonedArray = cloneArray(realArray);
          
        // Display both the real and cloned arrays
        displayArray(realArray, "Real Array");
        displayArray(clonedArray, "Cloned Array");
    }
  
    // Method to clone a 2D array
    private static int[][] cloneArray(int[][] original) {
        // Use Java 8 Streams to clone the array
        return Arrays.stream(original)
                .map(int[]::clone)
                .toArray(int[][]::new);
    }
  
    // Method to display a 2D array
    private static void displayArray(int[][] array,
                                     String message)
    {
        // Print a message indicating the purpose of the array
        System.out.println(message);
          
        // Iterate over each row of the array
        for (int[] row : array) {
            // Iterate over each element in the row and print it
            for (int element : row) {
                System.out.print(element + " ");
            }
            // Move to the next line after printing each row
            System.out.println();
        }
        // Print an empty line for clarity
        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 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads