Java program to merge two arrays one after the other

Program

import java.util.Arrays;
import java.util.Scanner;
public class ConcatenateTwoArraysWithoutarraycopy {
  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];
    int position = 0;
    for (int element : inputFirstArray) { 
      mergedArray[position] = element;
      position++; 
    }
    for (int element : inputSecondArray) { 
      mergedArray[position] = element;
      position++;
    }
    System.out.println("Arrays after merging: ");
    System.out.println(Arrays.toString(mergedArray));
  }
}

This Java program demonstrates how to merge two arrays such that the elements of the first array are followed by the elements of the second array. The merging is implemented without using the System.arraycopy method or any external libraries.

Use Case:

This method is suitable when merging arrays sequentially and maintaining full control over the merging logic, especially in learning or teaching scenarios.

  1. Input for the First Array:

    • The user specifies the size of the first array.
    • The program initializes an integer array, inputFirstArray[], of the specified size.
    • The user enters the elements, which are stored in the array.
  2. Input for the Second Array:

    • Similarly, the user specifies the size of the second array.
    • An integer array, inputSecondArray[], is initialized, and the user inputs its elements.
  3. Initialization of the Merged Array:

    • The combined size of the two input arrays is calculated (firstLen + secondLen).
    • A new array, mergedArray[], of this combined size is created.
  4. Merging the Arrays:

    • A for-each loop is used to iterate through the first array, and its elements are copied to the mergedArray starting from index 0.
    • A second for-each loop iterates through the second array, appending its elements to the mergedArray starting from the next available position.
  5. Output:

    • The merged array is printed using Arrays.toString() for easy readability.

Key Features:

  • Manual Merging:
    • The merging is done element by element using simple loops, providing clarity and control.
  • Dynamic Input:
    • The program supports arrays of any size, with elements provided by the user.
  • Efficiency:
    • The use of for-each loops ensures clean and easy-to-read code.

Example Explanation:

Input:

  • Array 1: [1, 2, 3]
  • Array 2: [4, 5, 6]

Process:

  1. The first array's elements are copied to mergedArray: [1, 2, 3].
  2. The second array's elements are appended to mergedArray: [1, 2, 3, 4, 5, 6].

Output:

  • Merged Array: [1, 2, 3, 4, 5, 6]

Advantages:

  • Straightforward Approach:
    • The logic is simple and easy to follow, especially for beginners.
  • No Dependencies:
    • The program avoids using external libraries or advanced Java features, focusing on basic array manipulation.

Output

javac .\ConcatenateTwoArraysWithoutarraycopy.java
java ConcatenateTwoArraysWithoutarraycopy        
Enter the required size of the first array: 
3
Enter the elements of the array: 
10
20
30
Enter the required size of the second array: 
2
Enter the elements of the array: 
40
50
Arrays after merging: 
[10, 20, 30, 40, 50