Java
Array Concatenation
Programming
Coding Tips
Java Array

How can I concatenate two arrays in Java?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Java does not have a built-in array concatenation operator. To combine two arrays, you must create a new array and copy elements from both sources. The main approaches are System.arraycopy() (fastest for primitives), Arrays.copyOf() combined with System.arraycopy(), the Stream API (cleanest for object arrays), and manual loops. For production code, Apache Commons Lang's ArrayUtils.addAll() provides a one-liner that handles null checks.

Method 1: System.arraycopy (Fastest)

System.arraycopy() is a native method that performs optimized memory copying:

java
1public static int[] concat(int[] a, int[] b) {
2    int[] result = new int[a.length + b.length];
3    System.arraycopy(a, 0, result, 0, a.length);
4    System.arraycopy(b, 0, result, a.length, b.length);
5    return result;
6}
7
8int[] arr1 = {1, 2, 3};
9int[] arr2 = {4, 5, 6};
10int[] combined = concat(arr1, arr2);
11// combined = [1, 2, 3, 4, 5, 6]

This is the fastest approach for primitive arrays because System.arraycopy uses native memory operations.

Method 2: Arrays.copyOf + System.arraycopy

Arrays.copyOf creates a new array with the contents of the first, then System.arraycopy appends the second:

java
1import java.util.Arrays;
2
3public static int[] concat(int[] a, int[] b) {
4    int[] result = Arrays.copyOf(a, a.length + b.length);
5    System.arraycopy(b, 0, result, a.length, b.length);
6    return result;
7}

This is slightly shorter than two System.arraycopy calls and produces the same result.

Method 3: Stream API (Java 8+)

The Stream API provides a clean, readable approach for both primitive and object arrays:

java
1import java.util.stream.IntStream;
2import java.util.stream.Stream;
3import java.util.Arrays;
4
5// Primitive int arrays
6int[] arr1 = {1, 2, 3};
7int[] arr2 = {4, 5, 6};
8int[] result = IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray();
9// [1, 2, 3, 4, 5, 6]
10
11// Object arrays (String, Integer, etc.)
12String[] words1 = {"hello", "world"};
13String[] words2 = {"foo", "bar"};
14String[] combined = Stream.concat(Arrays.stream(words1), Arrays.stream(words2))
15                         .toArray(String[]::new);
16// ["hello", "world", "foo", "bar"]

For double[] and long[], use DoubleStream.concat() and LongStream.concat() respectively.

Method 4: Generic Method with Reflection

A reusable generic method that works with any array type:

java
1import java.lang.reflect.Array;
2import java.util.Arrays;
3
4@SuppressWarnings("unchecked")
5public static <T> T[] concat(T[] a, T[] b) {
6    T[] result = Arrays.copyOf(a, a.length + b.length);
7    System.arraycopy(b, 0, result, a.length, b.length);
8    return result;
9}
10
11String[] names1 = {"Alice", "Bob"};
12String[] names2 = {"Charlie"};
13String[] allNames = concat(names1, names2);
14// ["Alice", "Bob", "Charlie"]
15
16Integer[] nums1 = {1, 2};
17Integer[] nums2 = {3, 4, 5};
18Integer[] allNums = concat(nums1, nums2);
19// [1, 2, 3, 4, 5]

This does not work with primitive arrays (int[], double[]). Use the primitive-specific methods above for those.

Method 5: Apache Commons Lang

If you already use Apache Commons Lang, ArrayUtils.addAll() is a one-liner with null safety:

xml
1<!-- Maven dependency -->
2<dependency>
3    <groupId>org.apache.commons</groupId>
4    <artifactId>commons-lang3</artifactId>
5    <version>3.14.0</version>
6</dependency>
java
1import org.apache.commons.lang3.ArrayUtils;
2
3int[] a = {1, 2, 3};
4int[] b = {4, 5, 6};
5int[] result = ArrayUtils.addAll(a, b);
6// [1, 2, 3, 4, 5, 6]
7
8// Handles null safely
9int[] safe = ArrayUtils.addAll(a, null);
10// [1, 2, 3]

Method 6: Manual Loop

Straightforward but verbose — useful when you cannot import utilities:

java
1public static int[] concat(int[] a, int[] b) {
2    int[] result = new int[a.length + b.length];
3    int pos = 0;
4    for (int val : a) result[pos++] = val;
5    for (int val : b) result[pos++] = val;
6    return result;
7}

Concatenating Multiple Arrays

To concatenate more than two arrays:

java
1@SafeVarargs
2public static <T> T[] concatAll(T[] first, T[]... rest) {
3    int totalLength = first.length;
4    for (T[] arr : rest) totalLength += arr.length;
5
6    T[] result = Arrays.copyOf(first, totalLength);
7    int offset = first.length;
8    for (T[] arr : rest) {
9        System.arraycopy(arr, 0, result, offset, arr.length);
10        offset += arr.length;
11    }
12    return result;
13}
14
15String[] a = {"a"}, b = {"b"}, c = {"c", "d"};
16String[] all = concatAll(a, b, c);
17// ["a", "b", "c", "d"]

Performance Comparison

MethodPrimitive ArraysObject ArraysNull SafeReadability
System.arraycopyBestGoodNoModerate
Arrays.copyOf + copyBestGoodNoModerate
Stream APIGoodGoodNoBest
Generic methodN/AGoodNoGood
ArrayUtils.addAllGoodGoodYesBest
Manual loopSlowestSlowestNoSimple

Common Pitfalls

  • Using + operator to concatenate arrays: Java does not support + for arrays. arr1 + arr2 produces a compile error (or unexpected string concatenation if used in a string context). You must explicitly create a new array and copy elements.
  • Forgetting that arrays are fixed-size: After creation, Java arrays cannot be resized. Concatenation always creates a new array. If you frequently add elements, use ArrayList instead and call list.addAll().
  • Using the generic method with primitive arrays: concat(int[], int[]) does not compile because generics do not work with primitives in Java. Write separate methods for int[], double[], long[], or use wrapper types (Integer[]).
  • Not handling null arrays: Passing null to System.arraycopy throws NullPointerException. If either array might be null, add null checks or use ArrayUtils.addAll() from Apache Commons.
  • Confusing Arrays.asList() with a mutable list: Arrays.asList(arr1) wraps the array but does not support addAll. To concatenate via lists, use new ArrayList<>(Arrays.asList(arr1)) and then call addAll.

Summary

  • System.arraycopy() is the fastest for primitive arrays — use it in performance-critical code
  • Stream.concat() / IntStream.concat() is the cleanest and most readable approach for Java 8+
  • Use a generic concat(T[], T[]) method for reusable object array concatenation
  • For null safety and convenience, use Apache Commons ArrayUtils.addAll()
  • If you need frequent concatenation, switch to ArrayList instead of arrays

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.