What are the different ways of copying an array into another array in Java?


In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0. You can access the elements of an array using name and position as −

System.out.println(myArray[3]);
//Which is 1457
Creating an array in Java:

In Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −

int myArray[] = new int[7];
myArray[0] = 1254;
myArray[1] = 1458;
myArray[2] = 5687;
myArray[3] = 1457;
myArray[4] = 4554;
myArray[5] = 5445;
myArray[6] = 7524;

Or, you can directly assign values with in flower braces separating them with commas (,) as −

int myArray = { 1254, 1458, 5687, 1457, 4554, 5445, 7524};

Copying arrays

You can copy one array from another in several ways −

Copying element by element − One way is to create an empty array with the length of the original array, and copy each element (in a loop).

Example

import java.util.Arrays;
public class CopyingArray {
   public static void main(String args[]) {
      //Source array (int)
      int integerArray1[] = { 1254, 1458, 5687, 1457, 4554, 5445, 7524};
      //Length of the array
      int length1 = integerArray1.length;
      //Creating an empty array
      int integerArray2[] = new int[length1];
      for (int i=0; i<length1; i++) {
         integerArray2[i] = integerArray1[i];
      }
      System.out.println("Original array: "+Arrays.toString(integerArray1));
      System.out.println("Copied array: "+Arrays.toString(integerArray2));
      //Source array (String)
      String StringArray1[] = { "Mango", "Apple", "Orange", "Banana", "Cherries"};
      //Length of the array
      int length2 = StringArray1.length;
      //Creating an empty array
      String StringArray2[] = new String[length2];
      for (int i=0; i<length2; i++) {
         StringArray2[i] = StringArray1[i];
      }
      System.out.println("Original array: "+Arrays.toString(StringArray1));
      System.out.println("Copied array: "+Arrays.toString(StringArray2));
   }
}

Output

Original array: [1254, 1458, 5687, 1457, 4554, 5445, 7524]
Copied array: [1254, 1458, 5687, 1457, 4554, 5445, 7524]
Original array: [Mango, Apple, Orange, Banana, Cherries]
Copied array: [Mango, Apple, Orange, Banana, Cherries]

Using the clone() method − The clone() method of the class java.lang.Object accepts an object as a parameter, creates and returns a copy of it.

Example

import java.util.Arrays;
public class CopyingArray {
   public static void main(String args[]) {
      //Source array (int)
      int integerArray1[] = { 1254, 1458, 5687, 1457, 4554, 5445, 7524};
      //Cloning the array
      int integerArray2[] = integerArray1.clone();
      System.out.println("Original array: "+Arrays.toString(integerArray1));
      System.out.println("Copied array: "+Arrays.toString(integerArray2));
      //Source array (String)
      String StringArray1[] = { "Mango", "Apple", "Orange", "Banana", "Cherries"};
      //Cloning the array
      String StringArray2[] = StringArray1.clone();
      System.out.println("Original array: "+Arrays.toString(StringArray1));
      System.out.println("Copied array: "+Arrays.toString(StringArray2));
   }
}

Output

Original array: [1254, 1458, 5687, 1457, 4554, 5445, 7524]
Copied array: [1254, 1458, 5687, 1457, 4554, 5445, 7524]
Original array: [Mango, Apple, Orange, Banana, Cherries]
Copied array: [Mango, Apple, Orange, Banana, Cherries]

Using the System.arraycopy() method − The copy() method of the System class accepts two arrays (along with other details) and copies the contents of one array to other.

Example

import java.util.Arrays;
public class CopyingArray {
   public static void main(String args[]) {
      //Source array (int)
      int integerArray1[] = { 1254, 1458, 5687, 1457, 4554, 5445, 7524};
      //Length of the array
      int length1 = integerArray1.length;
      //Destination array
      int integerArray2[] = new int[length1];
      //Copying Arrays
      System.arraycopy(integerArray1, 0, integerArray2, 0, length1);
      System.out.println("Original array: "+Arrays.toString(integerArray1));
      System.out.println("Copied array: "+Arrays.toString(integerArray2));
      //Source array (String)
      String StringArray1[] = { "Mango", "Apple", "Orange", "Banana", "Cherries"};
      //Length of the array
      int length2 = StringArray1.length;
      //Destination array
      String StringArray2[] = new String[length2];
      //Copying arrays
      System.arraycopy(StringArray1, 0, StringArray2, 0, length2);
      System.out.println("Original array: "+Arrays.toString(StringArray1));
      System.out.println("Copied array: "+Arrays.toString(StringArray2));
   }
}

Output

Original array: [1254, 1458, 5687, 1457, 4554, 5445, 7524]
Copied array: [1254, 1458, 5687, 1457, 4554, 5445, 7524]
Original array: [Mango, Apple, Orange, Banana, Cherries]
Copied array: [Mango, Apple, Orange, Banana, Cherries]
raja
Published on 02-Aug-2019 12:18:36
Advertisements