Java Program to Merge Two Arrays

In this article, you will learn the Java program to merge two arrays. Merging two arrays means combining both array elements into a single array. In order to merge two arrays, we will iterate both arrays one after the other and copy all the elements into the third array. Let’s see a few examples below:

Example#1. Merge two arrays in Java

package com.javacodepoint.array;

public class MergeArrayExample1 {

	public static void main(String[] args) {

		// Declare and Initialize first integer array
		int arr1[] = { 1, 2, 5, 6, 8 };

		// Declare and Initialize second integer array
		int arr2[] = { 5, 7, 10, 12, 20, 25 };

		// Declare another array to merge both array
		// The length of arr3 will be total sum of arr1 and arr2 length
		int arr3[] = new int[arr1.length + arr2.length];

		int count = 0;

		// Copy arr1 elements to arr3
		for (int i = 0; i < arr1.length; i++) {
			arr3[count++] = arr1[i];
		}

		// Copy arr2 elements to arr3
		for (int i = 0; i < arr2.length; i++) {
			arr3[count++] = arr2[i];
		}

		// Print the merged array elements
		System.out.println("Merged array elements: ");
		for (int i = 0; i < count; i++) {
			System.out.print(arr3[i] + " ");
		}

	}

}

OUTPUT:

Merged array elements:
1 2 5 6 8 5 7 10 12 20 25

Example#2. Merge two arrays in Java using Scanner

In this example, we will take inputs from users to initialize the array size and its element using java.lang.Scanner class.

package com.javacodepoint.array;

import java.util.Scanner;

public class MergeArrayExample2 {

	public static void main(String[] args) {

		// Creating scanner object for reading user inputs
		Scanner sc = new Scanner(System.in);

		// Read the first array size
		System.out.println("Enter the size of the first Array: ");
		int n = sc.nextInt();

		// Declare the first Array with given size
		int arr1[] = new int[n];

		// Reading the values for first Array
		System.out.println("Enter " + n + " element(s) for the first Array: ");
		for (int i = 0; i < n; i++) {
			arr1[i] = sc.nextInt();
		}

		// Read the second array size
		System.out.println("Enter the size of the second Array: ");
		n = sc.nextInt();

		// Declare the second Array with given size
		int arr2[] = new int[n];

		// Reading the values for second Array
		System.out.println("Enter " + n + " element(s) for the second Array: ");
		for (int i = 0; i < n; i++) {
			arr2[i] = sc.nextInt();
		}

		// Declare another array to merge both array
		// The size of arr3 will be total sum of arr1 and arr2 size
		int arr3[] = new int[arr1.length + arr2.length];

		int count = 0;

		// Copy arr1 elements to arr3
		for (int i = 0; i < arr1.length; i++) {
			arr3[count++] = arr1[i];
		}

		// Copy arr2 elements to arr3
		for (int i = 0; i < arr2.length; i++) {
			arr3[count++] = arr2[i];
		}

		// Print the merged array elements
		System.out.println("Merged array elements: ");
		for (int i = 0; i < count; i++) {
			System.out.print(arr3[i] + " ");
		}

	}

}

OUTPUT:

Enter the size of the first Array:
4
Enter 4 element(s) for the first Array:
2
4
5
7
Enter the size of the second Array:
3
Enter 3 element(s) for the second Array:
5
10
15
Merged array elements:
2 4 5 7 5 10 15

See also: Java Program to Copy an Array.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *