Java Programming

Efficient Array Concatenation in Java

Spread the love

Java doesn’t offer a built-in array concatenation operation. Since arrays have a fixed size, combining two arrays requires creating a new, larger array and copying the elements. This article explores three effective methods to achieve this.

Table of Contents

Using Apache Commons Lang’s addAll()

The Apache Commons Lang library provides the convenient addAll() method within its ArrayUtils class. This simplifies array concatenation significantly. First, add the dependency to your project (e.g., using Maven):

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version> <!-- Use the latest version -->
</dependency>

Then, use it as follows:

import org.apache.commons.lang3.ArrayUtils;

public class ArrayConcatenation {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};

        int[] concatenatedArray = ArrayUtils.addAll(array1, array2);

        for (int i : concatenatedArray) {
            System.out.print(i + " "); // Output: 1 2 3 4 5 6
        }
    }
}

This approach is clean and efficient but introduces an external dependency.

Leveraging System.arraycopy()

Java’s built-in System.arraycopy() provides a native solution. This method copies a portion of an array to another. It requires manual array creation and index management:

public class ArrayConcatenation {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};

        int[] concatenatedArray = new int[array1.length + array2.length];

        System.arraycopy(array1, 0, concatenatedArray, 0, array1.length);
        System.arraycopy(array2, 0, concatenatedArray, array1.length, array2.length);

        for (int i : concatenatedArray) {
            System.out.print(i + " "); // Output: 1 2 3 4 5 6
        }
    }
}

This method is efficient and avoids external libraries, but it’s less concise.

Manual Concatenation with Loops

A more explicit, though less efficient, approach involves iterating through both arrays and populating a new array:

public class ArrayConcatenation {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};

        int[] concatenatedArray = new int[array1.length + array2.length];

        for (int i = 0; i < array1.length; i++) {
            concatenatedArray[i] = array1[i];
        }
        for (int i = 0; i < array2.length; i++) {
            concatenatedArray[array1.length + i] = array2[i];
        }

        for (int i : concatenatedArray) {
            System.out.print(i + " "); // Output: 1 2 3 4 5 6
        }
    }
}

This method is straightforward and easy to understand, making it suitable for beginners. However, it’s less efficient than the other methods, especially for large arrays.

The optimal approach depends on your priorities: addAll() offers conciseness, arraycopy() provides efficiency without external libraries, and the manual loop offers clarity. Remember to adjust the code for different array types (e.g., String[], double[]).

Leave a Reply

Your email address will not be published. Required fields are marked *