Merge Array Program in Java
In this post, you will learn how to merge array in Java programming language.
An array is a data structure representing an ordered collection of indexed items. Array is used when there is a requirement to add more items in a single variable.
Merge is a common operation performed on multiple arrays, when two or more arrays are merged to form a bigger array containing all the items of the merged arrays.
For example, if you have two arrays [4, 2] and [1, 3], then merging these arrays results in [1, 2, 3, 4].
Java Program to merge two arrays using pre-defined function
Java arraycopy() is the method of the System class which belongs to the java.lang package. The arraycopy() method copies a source array from a specific beginning position to the destination array from the mentioned position. Here, we have used this method to merge two arrays.
import java.util.Arrays;
public class MergeArrayExample1
{
public static void main(String[] args)
{
int[] arr1 = {8,20,4,11,1};
int[] arr2 = {77,11,4,7,33,6};
//determines length of array
int arrlen1 = arr1.length;
int arrlen2 = arr2.length;
// resultant array
int[] result = new int[arrlen1 + arrlen2];
System.arraycopy(arr1, 0, result, 0, arrlen1);
System.arraycopy(arr2, 0, result, arrlen1, arrlen2);
// displaying the merged array
System.out.println(Arrays.toString(result));
}
}
Output of the above code:
[8, 20, 4, 11, 1, 77, 11, 4, 7, 33, 6]
Java Program to merge two arrays using for-each
In the following example, we have initialised two arrays, arr1 and arr2 of integer type. Manually copy each element of both arrays to merge_arr and convert that array into a string by using the toString() method of the Array class.
import java.io.*;
public class JavaMergeArray
{
public static void main(String[] args)
{
// first array
int arr1[] = { 9, 7, 3 };
// second array
int arr2[] = { 5,2,6,8,1 };
// determining length of first array
int arr_len1 = arr1.length;
// determining length of second array
int arr_len2 = arr2.length;
// resultant array size
int arrlen = arr_len1 + arr_len2;
// Creating a new array
int[] merge_arr = new int[arrlen];
// store the elements of first
// array into resultant array
for (int i = 0; i < arr_len1; i = i + 1) {
merge_arr[i] = arr1[i];
}
// concat the elements of second
// array into resultant array
for (int i = 0; i < arr_len2; i = i + 1) {
merge_arr[arr_len1 + i] = arr2[i];
}
// display the elements of
// resultant array
for (int i = 0; i < arrlen; i = i + 1) {
System.out.println(merge_arr[i]);
}
}
}
Output of the above code:
9
7
3
5
2
6
8
1
Java Program to merge two arrays using Collections
A collection represents a group of objects, known as its elements. Collections is a utility class that resides in the java.util package. It consists entirely of static methods which are used to operate on collections. In the given example, we have used the collection to merge arrays in Java.
import java.util. * ;
public class JavaMergeArray {
public static void main(String args[]) {
String str1[] = {"E","T","U","T","O"};
String str2[] = {"R","I","A","L","S"};
System.out.print("First array: ");
for (int i = 0; i < str1.length; i++) {
System.out.println(" " + str1[i]);
}
System.out.print("\nSecond array: ");
for (int i = 0; i < str2.length; i++) {
System.out.println(" " + str2[i]);
}
// getting a list view of an array
List list = new ArrayList(Arrays.asList(str1));
// adding all elements of str2 into list
list.addAll(Arrays.asList(str2));
//converting list to array
Object[] str3 = list.toArray();
System.out.println("\nMerged array: ");
System.out.println(Arrays.toString(str3));
}
}
Output of the above code:
First array: E
T
U
T
O
Second array: R
I
A
L
S
Merged array:
[E, T, U, T, O, R, I, A, L, S]
Java Program to merge two arrays using Stream API
Java provides a new additional package in Java 8 called java.util.stream. The Stream API provides many methods that are used to merge two arrays in Java.
import java.util.stream.Stream;
import java.util.Arrays;
import java.io.*;
public class JavaMergeArray
{
// function to merge two arrays
public static Object[] mergeArray(T[] array1, T[] array2)
{
return Stream.of(array1, array2).flatMap(Stream::of).toArray();
}
public static void main (String[] args)
{
Integer[] arr1 = new Integer[]{4,6,2,7,9};
Integer[] arr2 = new Integer[]{9,8,1,3,5};
//merged array
Object[] mergedArray = mergeArray(arr1,arr2);
System.out.println("Merged array: "+ Arrays.toString(mergedArray));
}
}
Output of the above code:
Merged array: [4, 6, 2, 7, 9, 9, 8, 1, 3, 5]
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java