Java Program to merge two arrays

Java Program to merge two arrays

In this tutorial we will learn writing Java Program to merge the two array and generate one new array.

This merging of array is also known as concatenation of arrays.

For Example:

Suppose we have two arrays arr1 = [2, 4, 6, 1, 3] and arr2 = [1, 3, 5, 2, 4]

So output will be arr3 = [2, 4, 6, 1, 3, 1, 3, 5, 2, 4]

Program 1: Merging two array using arraycopy() java function

  • In this program we are using java inbuilt method System.arraycopy() to perform array merging or concatenation.
  • Method looks like arraycopy(Object source_arr, int sourcePos, Object dest_arr, int destPos, int len)
import java.util.*;
public class Main{
    public static void main (String[]args){
    Scanner sc = new Scanner (System.in);
    
    //taking array size and array element as input for array1
    System.out.println ("Enter the size of array1: ");
    int size1 = sc.nextInt ();
    int arr1[] = new int[size1];
    for (int i = 0; i < size1; i++){
	    System.out.print ("Please give value for index " + i + " : ");
	    arr1[i] = sc.nextInt ();
    }
    
    //taking array size and array element as input for array1
    System.out.println ("Enter the size of array2: ");
    int size2 = sc.nextInt ();
    int arr2[] = new int[size2];
    for (int i = 0; i < size2; i++){
	    System.out.print ("Please give value for index " + i + " : ");
	    arr2[i] = sc.nextInt ();
    }
    
    //logic to merge two array
    //we are using arraycopy function
    int size3 = size1+size2;
    int resultArr[] = new int[size3];
    System.arraycopy(arr1, 0, resultArr, 0, size1);
    System.arraycopy(arr2, 0, resultArr, size1, size2);
    System.out.print ("Array after merging two array: "+Arrays.toString(resultArr));
    }
}

Output

Enter the size of array1: 
6
Please give value for index 0 : 3
Please give value for index 1 : 1
Please give value for index 2 : 2
Please give value for index 3 : 4
Please give value for index 4 : 3
Please give value for index 5 : 2 
Enter the size of array2: 
4
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 3
Please give value for index 3 : 4
Array after merging two array: [3, 1, 2, 4, 3, 2, 1, 2, 3, 4]

Program 2: Merge two array without using inbuilt java method

  • In this program we have built our own logic.
  • We have created three arrays here. First two array to take user input and third array to store result.
  • The result array will be of size, sum of first two array size.
  • Now using for loop we will be copying array element one by one to resultant array.
  • Once all element copied to resultant array, Just print it.
import java.util.*;
public class Main{
    public static void main (String[]args){
    Scanner sc = new Scanner (System.in);
    
    //taking array size and array element as input for array1
    System.out.println ("Enter the size of array1: ");
    int size1 = sc.nextInt ();
    int arr1[] = new int[size1];
    for (int i = 0; i < size1; i++){
	    System.out.print ("Please give value for index " + i + " : ");
	    arr1[i] = sc.nextInt ();
    }
    
    //taking array size and array element as input for array1
    System.out.println ("Enter the size of array2: ");
    int size2 = sc.nextInt ();
    int arr2[] = new int[size2];
    for (int i = 0; i < size2; i++){
	    System.out.print ("Please give value for index " + i + " : ");
	    arr2[i] = sc.nextInt ();
    }
    
    //logic to merge two array
    int size3 = size1+size2;
    int resultArr[] = new int[size3];
    
    //copying first array in resultArr
    for (int i = 0; i < size1; i++){
	    resultArr[i] = arr1[i];
    }
    
    //copying second array in resultArr
    for (int i = 0; i < size2; i++){
	    resultArr[i+size1] = arr2[i];
    }
    
    System.out.print ("Array after merging two array: "+Arrays.toString(resultArr));
    }
}

Output

Enter the size of array1: 
5
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 3
Please give value for index 3 : 4
Please give value for index 4 : 5
Enter the size of array2: 
4
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 3
Please give value for index 3 : 4
Array after merging two array: [1, 2, 3, 4, 5, 1, 2, 3, 4]