"Gluing" Arrays in Java: 5 Proven Ways (with Code)
Hi, colleague! If you're reading this text, you've encountered a task that every Java developer faces sooner or later — how to merge two arrays in Java? It seems simple, right? Just add them together. But it's not that straightforward. Java doesn't have a native operator for array concatenation (like Python or JavaScript). And that's where the fun begins.
On Stack Overflow, the question "How can I concatenate two arrays in Java?" has gathered hundreds of thousands of views. This means even experienced folks sometimes get lost in choosing the right approach. In this tutorial, I'll show you 5 working ways — from the "old-school" manual copying to the modern Stream API. You'll learn the pros and cons of each approach and can choose the one that fits your task.
Let's go!
Why is there no simple way to merge arrays in Java?
An array in Java is an object with a fixed length. You can't just "add" one array to the end of another. You need to create a new array and copy the elements into it. But don't worry — I'll show you how to do it elegantly and without pain.
Method 1: Manual Copying (for those who like full control)
The most obvious and straightforward method. We create a new array whose length is the sum of the lengths of the two original arrays, and fill it in a loop.
public class ArrayConcatManual { public static void main(String[] args) { int[] array1 = {1, 2, 3}; int[] array2 = {4, 5, 6};
int[] result = new int[array1.length + array2.length];
// Copy the first array for (int i = 0; i < array1.length; i++) { result[i] = array1[i]; }
// Copy the second array for (int i = 0; i < array2.length; i++) { result[array1.length + i] = array2[i]; }
// Print the result for (int num : result) { System.out.print(num + " "); } // Output: 1 2 3 4 5 6 }}Pros: full control, no external libraries required, easy to read.
Cons: verbose, easy to make mistakes with indices (especially if there are more than two arrays).
Tip: Use this method if you want to understand how everything works "under the hood," or if you're writing code for educational purposes.
Method 2: System.arraycopy() — The Standard Tool
Java has a built-in method System.arraycopy(). It works faster than a manual loop because it uses native code (native method). This is the recommended way for production.
import java.util.Arrays;
public class ArrayConcatSystem { public static void main(String[] args) { String[] fruits1 = {"apple", "banana"}; String[] fruits2 = {"cherry", "date"};
String[] result = new String[fruits1.length + fruits2.length];
System.arraycopy(fruits1, 0, result, 0, fruits1.length); System.arraycopy(fruits2, 0, result, fruits1.length, fruits2.length);
System.out.println(Arrays.toString(result)); // Output: [apple, banana, cherry, date] }}How it works:
- The first argument is the source array.
- The second is the starting position in the source array (usually 0).
- The third is the destination array.
- The fourth is the starting position in the destination array.
- The fifth is the number of elements to copy.
Pros: fast, compact, standard library.
Cons: need to keep track of indices, can throw ArrayIndexOutOfBoundsException.
Method 3: ArrayList and Collections — Flexibility and Convenience
If you need not just to merge arrays but also to work with them afterward (add, remove