Loading
Concatenation-tutorial
In Java, concatenation means combining or joining two arrays to form a new array that contains elements of both.



Use Case

When you want to merge two separate datasets stored in arrays into one for unified processing or output.



Example: Array Concatenation

package QuipoHouse.ArrayOperations;

public class Concatenation {
    public static void main(String[ ] args) {
        int A[ ] = { 1, 4, 9, 5, 7 };
        int B[ ] = { 2, 6, 10, 3, 8 };

        // Total length = length of A + B
        int len = A.length + B.length;
        int C[ ] = new int[len];

        // Copy elements from A to C
        for (int i = 0; i < len; i++) {
            if (i < A.length) {
                C[i] = A[i];
            } else {
                C[i] = B[i - A.length];
            }
        }

        // Print the concatenated array
        System.out.println("---Element after Concatenation---");
        System.out.print("[");
        for (int x : C) {
            System.out.print(x + " ");
        }
        System.out.print("]");
    }
}

Output:

---Element after Concatenation---
[1 4 9 5 7 2 6 10 3 8 ]



How It Works

  • Create a new array C[ ] of size A.length + B.length.
  • Loop through C and fill:
               1. First half with elements from A

               2. Second half with elements from B

  • Display the final array C which now contains both A and B values.