Java Concatenate Arrays

To concatenate arrays in Java, create a new array large enough to hold all input elements, then copy the first array followed by the second. Java arrays have a fixed length, so concatenation does not enlarge either original array.

You can do this with loops, Arrays.copyOf() and System.arraycopy(), Apache Commons ArrayUtils.addAll(), or the Stream API. The right approach depends on whether you are working with primitive arrays, object arrays, external libraries, or more than two arrays.

How Java Array Concatenation Preserves Element Order

If arr1 contains [1, 4, 9] and arr2 contains [16, 25, 36], concatenating them produces [1, 4, 9, 16, 25, 36]. The elements of the first array remain first, and the elements of the second array follow them.

The result length is arr1.length + arr2.length. Concatenation also keeps duplicate values unless you explicitly remove them. For arrays of objects, the array slots are copied; the referenced objects themselves are not deep-copied.

Example 1 – Concatenate Arrays – For Loop and Array Traversing

In the following program, we take two arrays, traverse them using Java For Loop, and create a new array with all the elements from these two input arrays. The resulting array will have a length equal to the sum of lengths of the two input arrays.

Java Program

</>
Copy
/**
 * Java Example Program, to Concatenate Arrays
 */
public class ConcatenateArrays {
	public static void main(String[] args) {
		//two arrays
		int[] arr1 = {1, 4, 9};
		int[] arr2 = {16, 25, 36};
		
		//concatenate arrays
		int[] result = new int[arr1.length+arr2.length];
		for(int i=0;i<arr1.length;i++) {
			result[i] = arr1[i];
		}
		for(int i=0;i<arr2.length;i++) {
			result[arr1.length+i] = arr2[i];
		}
		
		//print the result
		for(int element: result) System.out.println(element);
	}
}

Output

1
4
9
16
25
36

The first loop copies arr1 starting at index 0. The second loop starts writing at index arr1.length, so it continues immediately after the last element copied from the first array.

Example 2 – Concatenate Arrays – ArrayUtils.addAll()

In the following program, we will use ArrayUtils.addAll() method, to concatenate two arrays.

Java Program

</>
Copy
import org.apache.commons.lang.ArrayUtils;

/**
 * Java Example Program, to Concatenate Arrays
 */
public class ConcatenateArrays {
	public static void main(String[] args) {
		//two arrays
		int[] arr1 = {1, 4, 9};
		int[] arr2 = {16, 25, 36};
		//concatenate arrays
		int[] result = ArrayUtils.addAll(arr1, arr2);
		
		//print the result
		for(int element: result) System.out.println(element);
	}
}

Output

1
4
9
16
25
36

The example above uses the older Apache Commons Lang 2 package name, org.apache.commons.lang.ArrayUtils. With Apache Commons Lang 3, the class is in org.apache.commons.lang3.ArrayUtils. The addAll() method returns a new array containing the first array’s elements followed by the second array’s elements.

For Commons Lang 3, the corresponding import and call are:

</>
Copy
import org.apache.commons.lang3.ArrayUtils;

int[] result = ArrayUtils.addAll(arr1, arr2);

Example 3 – Concatenate Arrays – Arrays.copyOf() and System.arraycopy()

In the following program, we will use Arrays.copyOf() method, and System.arraycopy() method to concatenate two arrays.

Java Program

</>
Copy
import java.util.Arrays;

/**
 * Java Example Program, to Concatenate Arrays
 */
public class ConcatenateArrays {
	public static void main(String[] args) {
		//two arrays
		int[] arr1 = {1, 4, 9};
		int[] arr2 = {16, 25, 36};
		int[] result = Arrays.copyOf(arr1, arr1.length + arr2.length);
		System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
		
		//print the result
		for(int element: result) System.out.println(element);
	}
}

Output

1
4
9
16
25
36

Arrays.copyOf(arr1, arr1.length + arr2.length) creates the result array and copies all elements from arr1. The extra positions are then filled by System.arraycopy(arr2, 0, result, arr1.length, arr2.length). Here, 0 is the starting index in arr2, arr1.length is the destination index, and arr2.length is the number of elements to copy.

Concatenate Two int Arrays with IntStream.concat()

For primitive int[] arrays, Java’s Stream API provides IntStream.concat(). Convert each array to an IntStream, concatenate the streams, and call toArray() to create the result.

</>
Copy
import java.util.Arrays;
import java.util.stream.IntStream;

public class ConcatenateIntArrays {
    public static void main(String[] args) {
        int[] arr1 = {1, 4, 9};
        int[] arr2 = {16, 25, 36};

        int[] result = IntStream.concat(
                Arrays.stream(arr1),
                Arrays.stream(arr2)
        ).toArray();

        System.out.println(Arrays.toString(result));
    }
}

Output

[1, 4, 9, 16, 25, 36]

The same idea is available for long[] and double[] through LongStream.concat() and DoubleStream.concat(). For simple copying, System.arraycopy() is more direct; streams are useful when concatenation is part of a larger stream-processing operation.

Concatenate String Arrays with Stream.concat()

For reference-type arrays such as String[], use Stream.concat(). When converting the concatenated stream back to an array, provide an array constructor such as String[]::new.

</>
Copy
import java.util.Arrays;
import java.util.stream.Stream;

public class ConcatenateStringArrays {
    public static void main(String[] args) {
        String[] first = {"Java", "Kotlin"};
        String[] second = {"Scala", "Groovy"};

        String[] result = Stream.concat(
                Arrays.stream(first),
                Arrays.stream(second)
        ).toArray(String[]::new);

        System.out.println(Arrays.toString(result));
    }
}

Output

[Java, Kotlin, Scala, Groovy]

Concatenate More Than Two int Arrays in Java

When several arrays need to be concatenated, repeatedly nesting IntStream.concat() is unnecessary. A stream of arrays can be flattened into one IntStream, then collected into a new int[].

</>
Copy
import java.util.Arrays;
import java.util.stream.Stream;

public class ConcatenateMultipleArrays {
    public static void main(String[] args) {
        int[] arr1 = {1, 2};
        int[] arr2 = {3, 4};
        int[] arr3 = {5, 6};

        int[] result = Stream.of(arr1, arr2, arr3)
                .flatMapToInt(Arrays::stream)
                .toArray();

        System.out.println(Arrays.toString(result));
    }
}

Output

[1, 2, 3, 4, 5, 6]

Merge Two Java Arrays Without Duplicate Values

Concatenation and removing duplicates are separate operations. If you want a merged int[] containing each value only once, concatenate the streams and then apply distinct().

</>
Copy
import java.util.Arrays;
import java.util.stream.IntStream;

public class MergeArraysWithoutDuplicates {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4};
        int[] arr2 = {3, 4, 5, 6};

        int[] result = IntStream.concat(
                Arrays.stream(arr1),
                Arrays.stream(arr2)
        ).distinct().toArray();

        System.out.println(Arrays.toString(result));
    }
}

Output

[1, 2, 3, 4, 5, 6]

If duplicates are valid data and should be preserved, do not call distinct().

Java Array Concatenation vs ArrayList and String Joining

These operations are related but solve different problems:

  • Concatenate arrays: combine array elements into a new array of the same element type.
  • Add arrays to an ArrayList: use a collection when you need a resizable sequence and expect to add or remove elements later.
  • Join an array into a String: convert array elements into text separated by a delimiter; the result is a String, not another array.

For example, with String[] arrays, you can combine the elements in an ArrayList first and convert the list back to an array when needed.

</>
Copy
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

String[] first = {"A", "B"};
String[] second = {"C", "D"};

List<String> combined = new ArrayList<>(Arrays.asList(first));
combined.addAll(Arrays.asList(second));

String[] result = combined.toArray(new String[0]);

Which Java Array Concatenation Method Should You Use?

  • Use loops when you want the copying logic to be explicit or need custom processing for each element.
  • Use Arrays.copyOf() with System.arraycopy() for a direct standard-library solution that works with primitive and object arrays.
  • Use ArrayUtils.addAll() when Apache Commons Lang is already a dependency in the project and its utility method fits your codebase.
  • Use IntStream.concat(), LongStream.concat(), DoubleStream.concat(), or Stream.concat() when array concatenation is part of stream processing.
  • Use stream flattening when you need to concatenate several arrays, especially when subsequent stream operations such as filtering or distinct() are also required.

Java Array Concatenation Summary

In this Java Tutorial, we learned how to concatenate arrays in Java using loops, Apache Commons ArrayUtils.addAll(), Arrays.copyOf() with System.arraycopy(), and the Stream API. We also covered concatenating more than two arrays and merging arrays without duplicate values.