Java program to concatenate two arrays using Collections

Program

import java.util.Arrays;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class ConcatenateTwoArraysUsingCollections {
  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();
    String inputFirstArray[] = new String[size];
    System.out.println("Enter the elements of the array: ");
    for (i = 0; i < inputFirstArray.length; i++) {
      inputFirstArray[i] = reader.next();
    }
    System.out.println("Enter the required size of the second array: ");
    size = reader.nextInt();
    String inputSecondArray[] = new String[size];
    System.out.println("Enter the elements of the array: ");
    for (i = 0; i < inputSecondArray.length; i++) {
      inputSecondArray[i] = reader.next();
    }
    //Usgae of Java Collections to merge arrays
    List list = new ArrayList(Arrays.asList(inputFirstArray));
    list.addAll(Arrays.asList(inputSecondArray));
    Object[] mergedArray = list.toArray();
    System.out.println("Arrays after merging: ");
    System.out.println(Arrays.toString(mergedArray));
  }
}

This Java program demonstrates how to concatenate two arrays using Java Collections.

  1. Input First Array:

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

    • Similarly, the user is prompted to enter the size of the second array.
    • A String array inputSecondArray[] is created with the specified size.
    • The user enters the elements, which are stored in inputSecondArray.
  3. Concatenating Using Collections:

    • Convert First Array to List:
      • Arrays.asList(inputFirstArray) converts the first array into a list.
      • This list is passed to a new ArrayList to allow modifications (lists from Arrays.asList() are fixed-size).
    • Add Second Array to List:
      • list.addAll(Arrays.asList(inputSecondArray)) appends all elements from the second array to the list.
    • Convert List to Array:
      • list.toArray() converts the list back into an array (Object[]).
  4. Output:

    • The Arrays.toString() method is used to print the concatenated array in a readable format.

Example Explanation:

Input:

  • Array 1: ["A", "B", "C"]
  • Array 2: ["D", "E", "F"]

Process:

  1. Convert ["A", "B", "C"] to a modifiable ArrayList.
  2. Append ["D", "E", "F"] to the list.
  3. Convert the list back to an array: ["A", "B", "C", "D", "E", "F"].

Output:

  • Merged Array: ["A", "B", "C", "D", "E", "F"]

Output

javac .\ConcatenateTwoArraysUsingCollections.java
java ConcatenateTwoArraysUsingCollections        
Enter the required size of the first array: 
2
Enter the elements of the array: 
abc
def
Enter the required size of the second array: 
2
hij
klm
[abc, def, hij, klm]