Concatenate two arrays java – Java Program to Concatenate Two Arrays

Concatenate two arrays java: In the previous article, we have seen Java Program to Delete an Element from Desired Position of an Array

In this article we are going to see how we can concatenate two arrays in JAVA.

Java Program to Concatenate Two Arrays

Concatenate arrays 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 concatenate two arrays.

Method-1: Java Program to Concatenate Two Arrays By Copying Individual Elements

Approach:

  • Create two arrays with elements.
  • Create another array with size equal to the size of both arrays to hold elements of both the arrays.
  • Use loop to copy the elements from first array into the resultant array.
  • Repeat the above step for the second array.
  • Print the resultant array.

Program:

import java.util.Arrays;

public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr1[] = {12, 22, 34, 22, 54};
        int arr2[] = {29,54,98,87,2};
        //resultant array of size arr1.length+arr2.length
        int resultantArr[] = new int[arr1.length+arr2.length];
        int index = 0;
        // Using for loop to copy the elements from arr1
        for(int i:arr1)
            resultantArr[index++] = i;
        // Using for loop to copy the elements from arr2
        for(int i:arr2)
            resultantArr[index++] = i;    
        // Printing the array
        System.out.println("The array elements are : "+Arrays.toString(resultantArr));
        
    }
}

Output:

The array elements are : [12, 22, 34, 22, 54, 29, 54, 98, 87, 2]

Method-2: Java Program to Concatenate Two Arrays By Using System.arraycopy()

Approach:

  • Create two arrays with elements.
  • Create another array with enough size to hold elements of both the arrays.
  • Use System.arraycopy( ) to copy the small arrays into the empty array.
  • Print the resultant array.

Program:

import java.util.Arrays;

public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr1[] = {12, 22, 34, 22, 54};
        int arr2[] = {29,54,98,87,2};
        //resultant array of size arr1.length+arr2.length
        int resultantArr[] = new int[arr1.length+arr2.length];
        // Copy the array using arraycopy()
        System.arraycopy(arr1,0,resultantArr,0,arr1.length);
        System.arraycopy(arr2,0,resultantArr,arr1.length,arr2.length);
        // Printing the array
        System.out.println("The array elements are : "+Arrays.toString(resultantArr));
        
    }
}

Output:

The array elements are : [12, 22, 34, 22, 54, 29, 54, 98, 87, 2]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: