How to Merge Two Arrays in Java: Simple Methods Explained
To merge two arrays in Java, you can create a new array with a length equal to the sum of both arrays and copy elements from each array using
System.arraycopy. Alternatively, you can use Java 8+ Streams to concatenate arrays easily.Syntax
Here is the basic syntax to merge two arrays using System.arraycopy:
- Create a new array with length equal to the sum of the two arrays.
- Use
System.arraycopy(sourceArray, sourcePos, destArray, destPos, length)to copy elements from the first array. - Use
System.arraycopyagain to copy elements from the second array starting at the correct position.
For Java 8 and later, you can use Stream.concat to merge arrays:
Type[] merged = Stream.concat(Arrays.stream(array1), Arrays.stream(array2))
.toArray(Type[]::new);java
int[] merged = new int[array1.length + array2.length]; System.arraycopy(array1, 0, merged, 0, array1.length); System.arraycopy(array2, 0, merged, array1.length, array2.length);
Example
This example shows how to merge two integer arrays using System.arraycopy and print the merged array.
java
import java.util.Arrays; public class MergeArrays { public static void main(String[] args) { int[] array1 = {1, 2, 3}; int[] array2 = {4, 5, 6}; int[] merged = new int[array1.length + array2.length]; System.arraycopy(array1, 0, merged, 0, array1.length); System.arraycopy(array2, 0, merged, array1.length, array2.length); System.out.println(Arrays.toString(merged)); } }
Output
[1, 2, 3, 4, 5, 6]
Common Pitfalls
Common mistakes when merging arrays include:
- Not creating a new array with enough length to hold both arrays.
- Copying elements to the wrong position in the new array, causing overwrites or missing elements.
- Trying to merge arrays of different types without proper handling.
Also, using loops manually to copy elements is possible but less efficient and more error-prone than System.arraycopy.
java
/* Wrong way: new array too small */ int[] mergedWrong = new int[array1.length]; System.arraycopy(array1, 0, mergedWrong, 0, array1.length); System.arraycopy(array2, 0, mergedWrong, array1.length, array2.length); // Throws ArrayIndexOutOfBoundsException /* Right way: new array with correct size */ int[] mergedRight = new int[array1.length + array2.length]; System.arraycopy(array1, 0, mergedRight, 0, array1.length); System.arraycopy(array2, 0, mergedRight, array1.length, array2.length);
Quick Reference
Summary tips for merging arrays in Java:
- Use
System.arraycopyfor fast and safe copying. - For Java 8+, use
Stream.concatfor a concise approach. - Always create a new array with combined length.
- Use
Arrays.toString()to print arrays for debugging.
Key Takeaways
Create a new array with length equal to the sum of both arrays before merging.
Use System.arraycopy for efficient copying of array elements.
Java 8+ Streams provide a concise way to merge arrays with Stream.concat.
Avoid array index errors by carefully managing copy positions.
Use Arrays.toString to easily display merged arrays.