Merge two arrays java – Java Program to Merge Two Arrays

Merge two arrays java: In the previous article, we have seen Java Program to Find Maximum Sum of Two Integers in an Array of Integers

In this article we are going to see how to merge two arrays using java programming language.

Java Program to Merge Two Arrays

How to merge two integer arrays in java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to Find Maximum Sum of Two Integers in an Array of Integers.

Method-1: Java Program to Merge Two Arrays By Using for Loop and User Defined Method

Approach:

  1. Declare a new array with the size of both array (arr1.length + arr2.length).
  2. Copy first array (arr1) to new array from 0 to arr1.length
  3. Copy second array (arr2) to new array from arr1.length to (arr1.length + arr2.length).

Program:

import java.util.Arrays;

public class Main
{
    public static void main(String[] args) 
    {
        // initialize two arrays
        int[] arr1 = { 1, 3, 5, 7, 9 };
        int[] arr2 = { 2, 4, 6, 8, 10 };
        //mergeArrays() method called 
        mergeArrays(arr1, arr2);
    }
    
    //mergeArrays() method to merge two arrays
    public static void mergeArrays(int[] arr1, int[] arr2) 
    {
        // create new array
        int newArray[] = new int[arr1.length + arr2.length];

        // Copy first to new array from 0 to src1.length
        for (int i = 0; i < arr1.length; i++) 
        {
            newArray[i] = arr1[i];
        }

        // copy second array to new array
        for (int i = 0, j = arr1.length; j < (arr1.length + arr2.length); j++, i++) 
        {
            newArray[j] = arr2[i];
        }

        // display all array
        System.out.println("Array1 = " + Arrays.toString(arr1));
        System.out.println("Array2 = " + Arrays.toString(arr2));
        System.out.println("Merged Array = " + Arrays.toString(newArray));
    }
}

Output:

Array1 = [1, 3, 5, 7, 9]
Array2 = [2, 4, 6, 8, 10]
Merged Array = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]

Method-2: Java Program to Merge Two Arrays By Using System.arraycopy() method and Scanner class

Approach:

  1. Create scanner class object.
  2. Ask use length of the array.
  3. Initialize the array with given size.
  4. Ask the user for array elements.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // initialize two arrays
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of arrays: ");
        int n = sc.nextInt();
        int m = sc.nextInt();
        // initialize array with size n
        int[] arr1 = new int[n];
        int[] arr2 = new int[m];

        // take input from user for array elements
        System.out.print("Enter array 1 elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr1[i] = sc.nextInt();
        }
        System.out.print("Enter array 2 elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr2[i] = sc.nextInt();
        }
        mergeArraysMethod(arr1, arr2);
    }

    public static void mergeArraysMethod(int[] arr1, int[] arr2) 
    {
        // create new array
        int newArray[] = new int[arr1.length + arr2.length];

        // Copy first to new array from 0 to src1.length
        System.arraycopy(arr1, 0, newArray, 0, arr1.length);

        // copy second array to new array
        System.arraycopy(arr2, 0, newArray, arr1.length, arr2.length);

        // display all array
        System.out.println("Array1 = " + Arrays.toString(arr1));
        System.out.println("Array2 = " + Arrays.toString(arr2));
        System.out.println("Merged Array = " + Arrays.toString(newArray));
    }
}
Output:

Enter the size of arrays: 3
2
Enter array 1 elements: 1 2 3
Enter array 2 elements: 10 20
Array1 = 1 2 3
Array2 = 10 20
Merged Array = 1 2 3 10 20

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: