Java Program to Merge Two Arrays
Given two arrays, the task is to merge or concatenate them and store the result into another array.
Example:
// Java Program to demonstrate merging
// two array using System.arraycopy() method
import java.util.Arrays;
public class MergeTwoArrays1 {
public static void main(String[] args) {
int[] a = { 10, 20, 30, 40 };
int[] b = { 50, 60, 70, 80 };
// determines length of both arrays
int a1 = a.length;
int b1 = b.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(a, 0, c, 0, a1);
System.arraycopy(b, 0, c, a1, b1);
System.out.println("" + Arrays.toString(c));
}
}
Output
[10, 20, 30, 40, 50, 60, 70, 80]
Explanation: The simplest and most efficient way to merge two arrays is by using the in-built System.arraycopy()
function.
- First, we initialize two arrays,
a
andb
, and store values in them. - Then, we calculate their lengths and create a third array to hold the merged result.
- Then we use
System.arraycopy()
to merge both arrays into the third array.
Other Methods to Merge Two Arrays
1. Without Using Pre-Defined Function
This method is useful when we need full control over the merging process without relying on built-in functions. It is better than a user-defined function because it avoids extra function overhead. It directly merging the arrays.
// Java Program to demonstrate merging
// two array without using pre-defined method
import java.util.Arrays;
public class MergeTwoArrays2 {
public static void main(String[] args) {
int a[] = { 10, 20, 30 };
int b[] = { 40, 50, 60, 70, 80 };
// determining length of both arrays
int a1 = a.length;
int b1 = b.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] = a[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] = b[i];
}
System.out.println("" + Arrays.toString(c));
}
}
Output
[10, 20, 30, 40, 50, 60, 70, 80]
Explanation:
- First, we initialize two arrays,
a
andb
. Then we
calculate their lengths.
- Then, we create a new array
c
to store the merged result. - The first loop copies elements from
a
toc
and the second loop copies elements fromb
toc
. - At last we print the merged array.
2. Using Java Streams
This method utilizes Java Streams to concatenate or merge two arrays. Java Streams were introduced in Java 8 and provide a declarative way to process collections of elements.
// Java Program to demonstrate merging
// two array using Java Stream
import java.util.Arrays;
import java.util.stream.IntStream;
public class MergeTwoArrays3 {
public static void main(String[] args) {
int a[] = {10, 20, 30};
int b[] = {40, 50, 60, 70, 80};
// Merging arrays using Java Streams
int[] c = mergeArraysUsingStreams(a, b);
System.out.println("" + Arrays.toString(c));
}
public static int[] mergeArraysUsingStreams(int[] arr1, int[] arr2) {
return IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray();
}
}
Output
[10, 20, 30, 40, 50, 60, 70, 80]
Explanation:
- In this example, we merge two integer arrays
a
andb
using Java Streams by concatenating them withIntStream.concat()
method.
- Then we convert the result back to an array.
- The
mergeArraysUsingStreams
method combines the two arrays and returns the merged array as anint[]
.
3. Using ArrayList
In this method, we have used an ArrayList to facilitate the merging of two arrays. An ArrayList is a dynamic data structure in Java that can grow or shrink in size as needed.
// Java Program to demonstrate merging
// two array using ArrayList
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MergeTwoArrays4 {
public static int[] mergeArraysUsingArrayList(int[] a,
int[] b)
{
// Create an ArrayList to store the merged
// elements
List<Integer> r = new ArrayList<>();
// Iterate through a and add each element to
// resultList
for (int n : a) {
r.add(n);
}
// Iterate through b and add each element to
// resultList
for (int n : b) {
r.add(n);
}
// Convert the ArrayList to an array using
// streams
return r.stream()
.mapToInt(Integer::intValue).toArray();
}
public static void main(String[] args)
{
int a[] = { 10, 20, 30 };
int b[] = { 40, 50, 60, 70, 80 };
int[] r1 = mergeArraysUsingArrayList(a, b);
System.out.println("" + Arrays.toString(r1));
}
}
Output
[10, 20, 30, 40, 50, 60, 70, 80]
Explanation:
- In this example, we use an
ArrayList
to store the merged elements from two arrays. - The
for-each
loop iterates through both arrays by adding their elements to theArrayList
. - Then the
stream().mapToInt(Integer::intValue).toArray()
method converts theArrayList
back into an integer array and prints the merged result.