Java program to concatenate two arrays using array copy method

Program

import java.util.Arrays;
import java.util.Scanner;
public class ConcatenateTwoArraysUsingarraycopy
{
    public static void main(String[] args) {
        int i;
        System.out.println("Enter the required size of the first array: ");
        Scanner reader = new Scanner(System.in);
        int size = reader.nextInt();
        int[] inputFirstArray = new int[size];
        System.out.println("Enter the elements of the array: ");
        for (i = 0; i < size; i++) {
            inputFirstArray[i] = reader.nextInt();
        }
        System.out.println("Enter the required size of the second array: ");      
        size = reader.nextInt();
        int[] inputSecondArray = new int[size];
        System.out.println("Enter the elements of the array: ");
        for (i = 0; i < size; i++) {
            inputSecondArray[i] = reader.nextInt();
        }
        int firstLen = inputFirstArray.length;
        int secondLen = inputSecondArray.length;
        int[] mergedArray = new int[firstLen + secondLen];
        System.arraycopy(inputFirstArray, 0, mergedArray, 0, firstLen);
        System.arraycopy(inputSecondArray, 0, mergedArray, firstLen, secondLen);
        System.out.println("Arrays after merging: ");
        System.out.println(Arrays.toString(mergedArray));
    }
}

This Java program concatenates two arrays using the System.arraycopy() method.

  1. Input First Array:

    • The user is prompted to enter the size of the first array (size).
    • An integer array inputFirstArray[] is created with the specified size.
    • The user enters the elements, which are stored in inputFirstArray.
  2. Input Second Array:

    • The user is prompted to enter the size of the second array (size).
    • A second integer array inputSecondArray[] is created with the specified size.
    • The user enters the elements, which are stored in inputSecondArray.
  3. Determine Array Sizes:

    • The lengths of the two input arrays are stored in firstLen and secondLen.
  4. Concatenate Arrays:

    • A new array mergedArray[] is created with a size equal to the sum of the lengths of the two input arrays.
    • The System.arraycopy() method is used to copy the elements of the first and second arrays into mergedArray:
      • System.arraycopy(inputFirstArray, 0, mergedArray, 0, firstLen):
        • Copies all elements from inputFirstArray starting at index 0 into mergedArray starting at index 0.
      • System.arraycopy(inputSecondArray, 0, mergedArray, firstLen, secondLen):
        • Copies all elements from inputSecondArray starting at index 0 into mergedArray starting at index firstLen.
  5. Output:

    • The Arrays.toString() method is used to print the contents of the concatenated array (mergedArray).

Example:

  • Input Arrays: [1, 2, 3] and [4, 5, 6]
  • Concatenated Array: [1, 2, 3, 4, 5, 6]

Output

javac .\ConcatenateTwoArraysUsingarraycopy.java
java ConcatenateTwoArraysUsingarraycopy        
Enter the required size of the first array: 
3
Enter the elements of the array: 
11
22
33
Enter the required size of the second array:
3
Enter the elements of the array:
44
55
66
Arrays after merging:
[11, 22, 33, 44, 55, 66]