Open In App

Java Program to Merge Two Arrays

Last Updated : 21 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, merging two arrays is a good programming question. We have given two arrays, and our task is to merge them, and after merging, we need to put the result back into another array. In this article, we are going to discuss different ways we can use to merge two arrays in Java.

Let's now see the real implementation of this for better understanding.

Example: In the below example, we are merging two arrays into a new array.

Java
// Java program to merge two array 
// using System.arraycopy() method
import java.util.Arrays;

public class Geeks {
    public static void main(String[] args) {

        int[] arr1 = { 10, 20, 30, 40 };
        int[] arr2 = { 50, 60, 70, 80 };

        // determines length of both arrays
        int a1 = arr1.length;
        int b1 = arr2.length;

        // resultant array size
        int c1 = a1 + b1;

        // create the resultant array
        int[] c = new int[c1];

        // using the pre-defined function arraycopy
        System.arraycopy(arr1, 0, c, 0, a1);
        System.arraycopy(arr2, 0, c, a1, b1);

        System.out.println("" + Arrays.toString(c));
    }
}

Output
[10, 20, 30, 40, 50, 60, 70, 80]

Explanation: In the above example, we are using in-built System.arraycopy() method to merge two arrays. Here, we are initalizing two arrays arr1 and arr2 and inserting values in them and then we are calculating the length. Then we create another array which will hold the merged result and the last step is to use System.arraycopy() to merge both arrays into the third array.

Methods to Merge Two Arrays

Now, we are going to discuss some methods to merge two arrays which are listed below:

1. Without Using Pre-Defined Function: This method is used for merging two arrays manually. It gives us full control over the process and does not rely on built-in functions.

Example:

Java
// Java Program to demonstrate merging
// two array without using pre-defined method
import java.util.Arrays;

public class Geeks
{
    public static void main(String[] args)
    {

        int arr1[] = { 10, 20, 30 };
        int arr2[] = { 40, 50, 60, 70, 80 };

        // determining length of both arrays
        int a1 = arr1.length;
        int b1 = arr2.length;

        // resultant array size
        int c1 = a1 + b1;

        // Creating a new array
        int[] c = new int[c1];

        // Loop to store the elements of first
        // array into resultant array
        for (int i = 0; i < a1; i = i + 1) {

            // Storing the elements in
            // the resultant array
            c[i] = arr1[i];
        }

        // Loop to concat the elements of second
        // array into resultant array
        for (int i = 0; i < b1; i = i + 1) {

            // Storing the elements in the
            // resultant array
            c[a1 + i] = arr2[i];
        }

        System.out.println("" + Arrays.toString(c));
    }
}

Output
[10, 20, 30, 40, 50, 60, 70, 80]

Explanation: In this example, we are initalizing two arrays, arr1 and arr2 and then we are creating another array c which will store the merged result. The first loop copies elements from arr1 to c and the second loop copies elements from arr2 to c and in the end we are printing the merged array.

2. Using Java Streams: This method is used to merge two arrays with the help of Java Streams.

Example:

Java
// Java program to demonstrate merging
// two array using Java Stream
import java.util.Arrays;
import java.util.stream.IntStream;

public class Geeks
{
    public static void main(String[] args) {

        int arr1[] = {10, 20, 30};
        int arr2[] = {40, 50, 60, 70, 80};

        // Merging arrays using Java Streams
        int[] ans  = mergeArraysUsingStreams(arr1, arr2);

        System.out.println("" + Arrays.toString(ans));
    }

    public static int[] mergeArraysUsingStreams(int[] arr1, int[] arr2) {

        // Using Java Stream
        return IntStream.concat(Arrays.stream(arr1), 
        Arrays.stream(arr2)).toArray();
    }
}

Output
[10, 20, 30, 40, 50, 60, 70, 80]

Explanation: Here, we are merging two arrays with the help of IntStream.concat() method and then the result is converted back into an array. We are using mergeArraysUsingStreams method because it combines the two arrays and returns the merged array as an int[].

3. Using ArrayList: This method uses ArrayList to merge two arrays. An ArrayList is a linear data structure in Java which can automatically increases its size when needed.

Example:

Java
// Java program to demonstrate merging
// two array using ArrayList
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Geeks
{

    public static int[] mergeArraysUsingArrayList(int[] a, int[] b)
    {

        // Create an ArrayList to store the merged
        // elements
        List<Integer> l = new ArrayList<>();

        // Iterate through a and add each element to
        // resultList
        for (int n : a) {
            l.add(n);
        }

        // Iterate through b and add each element to
        // resultList
        for (int n : b) {
            l.add(n);
        }

        // Convert the ArrayList to an array using
        // streams
        return l.stream()
                .mapToInt(Integer::intValue).toArray();
    }

    public static void main(String[] args)
    {

        int arr1[] = { 10, 20, 30 };
        int arr2[] = { 40, 50, 60, 70, 80 };

        int[] ans = mergeArraysUsingArrayList(arr1, arr2);

        System.out.println("" + Arrays.toString(ans));
    }
}

Output
[10, 20, 30, 40, 50, 60, 70, 80]

Explanation: Here, ArrayList is holding all the elements from the two merged arrays. Then for-each loop is used to iterate over the elements of an array and then we are converting the ArrayList back into a regular array and in the end we are printing the merged array.


Next Article

Similar Reads