Merge Two Arrays in Java

How to merge two arrays in Java? Merging of two arrays in Java also can be done by using pre-defined methods, or we can do it manually by using loops. Let us discuss them one by one.

Example of merging of two int arrays,
Array1 = [10, 20, 30, 40, 50]
Array2 = [9, 18, 27, 36, 45]
Then the Result should be,
Merged Array = [10, 20, 30, 40, 50, 9, 18, 27, 36, 45]

Example of merging of two String array,
Array1 = [Java, Python, C++]
Array2 = [HTML, CSS, JavaScript]
Then the Result should be,
Merged Array = [Java, Python, C++, HTML, CSS, JavaScript]

Merge Two Arrays in Java using Loops

Steps to combine two arrays in Java,
a) Take two array which will be merged, assume src1 and src2.
b) Declare a new array with the size of both array (src1.length + src2.length ).
c) Copy first array (src1) to new array from 0 to src1.length-1
d) Copy second array (src2) to new array from src1.length to (src1.length + src2.length).

Now, let us demonstrate it through an example. Merge two arrays without using a pre-defined method.

import java.util.Arrays;

public class CopyArray {

   public static void main(String[] args) {
      
      // array which should be merged
      int src1[] = {10, 20, 30, 40, 50};
      int src2[] = {9, 18, 27, 36, 45};
      
      // create new array 
      int newArray[] = new int[src1.length + src2.length];
      
      // Copy first to new array from 0 to src1.length
      for(int i=0; i<src1.length; i++) {
         newArray[i] = src1[i];
      }
      
      // copy second array to new array
      for(int i=0, j=src1.length; j<(src1.length + src2.length); 
                                                      j++, i++) {
         newArray[j] = src2[i];
      }
      
      // display all array
      System.out.println("Array1 = " + Arrays.toString(src1));
      System.out.println("Array2 = " + Arrays.toString(src2));
      System.out.println("Merged Array = " 
                             + Arrays.toString(newArray));
   }
}

Output:-

Array1 = [10, 20, 30, 40, 50]
Array2 = [9, 18, 27, 36, 45]
Merged Array = [10, 20, 30, 40, 50, 9, 18, 27, 36, 45]

In this program, to display the array we have used the toString() method of java.util.Arrays class that is given to convert array to String.

Merge Two Array using System.arraycopy() method

In place of loops, we can also use the pre-defined method System.arraycopy(). The System.arraycopy() method in Java is given to copy an array to another array. It copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Syntax of the arraycopy() method in java.lang.System class:- public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

Parameters in this method:-

  • src:- The source array.
  • srcPos:- Starting position in the source array.
  • dest:- The destination array.
  • destPos:- starting position in the destination array.
  • length:- the number of array elements to be copied.

Since java.lang.System class is the imported by default in all Java classes therefore to use arraycopy() method no need to explicitly import the System class.

import java.util.Arrays;

public class CopyArray {

   public static void main(String[] args) {
      
      // array which should be merged
      int src1[] = {10, 20, 30, 40, 50};
      int src2[] = {9, 18, 27, 36, 45};
      
      // create new array 
      int newArray[] = new int[src1.length + src2.length];
      
      // Copy first to new array from 0 to src1.length
      System.arraycopy(src1, 0, newArray, 0, src1.length);
      
      // copy second array to new array
      System.arraycopy(src2, 0, newArray, src1.length, src2.length);
      
      // display all array
      System.out.println("Array1 = " + Arrays.toString(src1));
      System.out.println("Array2 = " + Arrays.toString(src2));
      System.out.println("Merged Array = " 
                             + Arrays.toString(newArray));
   }
}

Output:-

Array1 = [10, 20, 30, 40, 50]
Array2 = [9, 18, 27, 36, 45]
Merged Array = [10, 20, 30, 40, 50, 9, 18, 27, 36, 45]

How to Merge Two String Arrays in Java

In the previous examples, we were merging the int array. Now, let us see those programs by merging two String arrays.

import java.util.Arrays;

public class CopyArray {

   public static void main(String[] args) {
      
      // array which should be merged
      String src1[] = {"Java", "Python", "C++"};
      String src2[] = {"HTML", "CSS", "JavaScript"};
      
      // create new array 
      String newArray[] = new String[src1.length + src2.length];
      
      // Copy first to new array from 0 to src1.length
      System.arraycopy(src1, 0, newArray, 0, src1.length);
      
      // copy second array to new array
      System.arraycopy(src2, 0, newArray, src1.length, src2.length);
      
      // display all array
      System.out.println("Array1 = " + Arrays.toString(src1));
      System.out.println("Array2 = " + Arrays.toString(src2));
      System.out.println("Merged Array = " 
                             + Arrays.toString(newArray));
   }
}

Output:-

Array1 = [Java, Python, C++]
Array2 = [HTML, CSS, JavaScript]
Merged Array = [Java, Python, C++, HTML, CSS, JavaScript]

Leave a Reply