How to Merge Two Arrays in Java

 

How to Merge Two Arrays in Java

How to merge two arrays in java?

In the following program, two int arrays merge into a third array.

Merging of two int array in Java

public class MergeArrayNumber

{

   public static void main(String args[])

   {

       int arr1[]={1,2,3,4,5};  // First Array

       int arr2[]={6,7,8,9,10}; // Second Array

       int arr3[]=new int[10];  // Third Array

       int i, j, k=0;

       // Print the first Array

       System.out.println("First Array");

       for(i=0; i<5; i++)

       {

           System.out.print(arr1[i] + " ");

       }

       System.out.println();

       // Print the second Array

       System.out.println("Second Array");

       for(i=0; i<5; i++)

       {

           System.out.print(arr2[i] + " ");

       }

       System.out.println();

       // Merging of two array

       // First array into the third array

       for(i=0; i<5; i++)

       {

           arr3[k] = arr1[i]; // transferring arr1 value into arr3

           k++;

       }

       // Second array into the third array

       for(i=0; i<5; i++)

       {

           arr3[k]=arr2[i];  // transferring arr2 value into arr3

           k++;

       }

       System.out.println("After Merging of Two arrays into third array");

       for(i=0; i<k; i++)

       {

           System.out.print(arr3[i] + " ");

        }

        System.out.println();      

    }       

}

Output

First Array

1 2 3 4 5

Second Array

6 7 8 9 10

After Merging of Two arrays into third array

1 2 3 4 5 6 7 8 9 10

How to merge two String arrays in java?

In the following program, two String arrays merge into a third String array.

public class StringMergeArray

{

   public static void main(String args[])

   {

       String arr1[]={"Khurshid", "Sachin", "Umesh", "Salim", "Junaid"};  // First String Array

       String arr2[]={"Dhoni", "Harshit", "Gagan", "Shakil", "Habib"}; // Second String Array

       String arr3[]=new String[10];  // Third Array

       int i, j, k=0;

       System.out.println("First Array");

       for(i=0; i<5; i++)

       {

           System.out.print(arr1[i] + " ");

       }

       System.out.println();

       System.out.println("\nSecond Array");

       for(i=0; i<5; i++)

       {

           System.out.print(arr2[i] + " ");

       }

       System.out.println();

       // Merging of two String array

       // First array into the third array

       for(i=0; i<5; i++)

       {

           arr3[k]=arr1[i];

           k++;

       }

       // Second array into the third array

       for(i=0; i<5; i++)

       {

           arr3[k]=arr2[i];

           k++;

       }

       System.out.println("\nAfter Merging of Two arrays into third array");

       for(i=0; i<k; i++)

       {

           System.out.print(arr3[i] + " ");

        }

        System.out.println();      

    }       

}

Output

First Array

Khurshid Sachin Umesh Salim Junaid

 Second Array

Dhoni Harshit Gagan Shakil Habib

 After Merging of Two arrays into the third array

Khurshid Sachin Umesh Salim Junaid Dhoni Harshit Gagan Shakil Habib

 In the above program two String arrays array1 and array2 merge into String array3.

Merging of two arrays with method or function in java

 import java.util.*;

public class MergeArrayWithMethod

{

    int i, k;

    int array3[] = new int[10];

    MergeArrayWithMethod()

    {

        k=0;

    }

    void mergeArray(int array1[], int array2[])

    {

        for(i=0; i<5; i++)

        {

            array3[k] = array1[i];

            k++;

        }

        for(i=0; i<5; i++)

        {

            array3[k] = array2[i];

            k++;

        }

    }

    void printArray()

    {

        System.out.println("After merging");

        for(i=0; i<k; i++)

        {

            System.out.print(array3[i] + " ");

        }

    }

    public static void main(String args[])

    {

        // Object of Scanner class

        Scanner sc = new Scanner(System.in);

        // Object of MergeArrayWithMethod class

        MergeArrayWithMethod ob = new MergeArrayWithMethod();

        int array1[] = new int[5];

        int array2[] = new int[5];

        int i;

        System.out.println("Enter elements in Array 1 ");

        for(i=0 ;i<5; i++)

        {

            array1[i] = sc.nextInt();

        }

        System.out.println("Enter elements in Array 2 ");

        for(i=0 ;i<5; i++)

        {

            array2[i] = sc.nextInt();

        }

        ob.mergeArray(array1, array2);

        ob.printArray();

    } 

}

Output

Enter elements in Array 1

7

8

9

4

5

Enter elements in Array 2

1

2

3

6

10

After merging

7 8 9 4 5 1 2 3 6 10

Java Program to Concatenate Two Arrays using copyarray method

Concatenate Two Arrays using arraycopy

import java.util.Arrays;

 

public class ConcatArray

{

    public static void main(String[] args) {

        int array1[] = {11, 22, 33, 44, 55};

        int array2[] = {66, 77, 88, 99};

 

        int length1 = array1.length;

        int length2 = array2.length;

        int arrConcat[] = new int[length1 + length2];

        // using arraycopy method

        System.arraycopy(array1, 0, arrConcat, 0, length1);

        System.arraycopy(array2, 0, arrConcat, length1, length2);

        // print the merge array

        System.out.println(Arrays.toString(arrConcat));

    }

}

Output

[11, 22, 33, 44, 55, 66, 77, 88, 99]

in the above program, we merge two arrays using the java collection method arraycopy into third array. 'arraycopy’ method is from java.util.Arrays class. 


More tutorial and examples of java

Loops in Java - Java loop explained with examples




Java program for Pronic Number



SHARE THIS
Previous Post
Next Post