How can I concatenate two arrays in Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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:
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:
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:
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:
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:
Method 6: Manual Loop
Straightforward but verbose — useful when you cannot import utilities:
Concatenating Multiple Arrays
To concatenate more than two arrays:
Performance Comparison
| Method | Primitive Arrays | Object Arrays | Null Safe | Readability |
System.arraycopy | Best | Good | No | Moderate |
Arrays.copyOf + copy | Best | Good | No | Moderate |
| Stream API | Good | Good | No | Best |
| Generic method | N/A | Good | No | Good |
ArrayUtils.addAll | Good | Good | Yes | Best |
| Manual loop | Slowest | Slowest | No | Simple |
Common Pitfalls
- Using
+operator to concatenate arrays: Java does not support+for arrays.arr1 + arr2produces 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
ArrayListinstead and calllist.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 forint[],double[],long[], or use wrapper types (Integer[]). - Not handling null arrays: Passing
nulltoSystem.arraycopythrowsNullPointerException. If either array might be null, add null checks or useArrayUtils.addAll()from Apache Commons. - Confusing
Arrays.asList()with a mutable list:Arrays.asList(arr1)wraps the array but does not supportaddAll. To concatenate via lists, usenew ArrayList<>(Arrays.asList(arr1))and then calladdAll.
Summary
System.arraycopy()is the fastest for primitive arrays — use it in performance-critical codeStream.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
ArrayListinstead of arrays

