Merging array in Java
Merging two arrays is a common task in programming that involves combining the elements of two separate arrays into a single array. In Java, there are various methods to achieve this, each with its own advantages. In this article, we will explore different techniques to merge two arrays, complete with code examples.
Method 1. Using System.arraycopy()
The simplest and most efficient way to merge two arrays in Java is by using the built-in System.arraycopy() method. This method allows you to copy elements from one array to another efficiently.
Code example
import java.util.Arrays;
public class MergeArraysUsingArraycopy {
public static void main(String[] args) {
int[] array1 = {10, 20, 30, 40};
int[] array2 = {50, 60, 70, 80};
// Determine the length of both arrays
int length1 = array1.length;
int length2 = array2.length;
// Create a new array to hold the merged result
int[] mergedArray = new int[length1 + length2];
// Copy elements from the first array
System.arraycopy(array1, 0, mergedArray, 0, length1);
// Copy elements from the second array
System.arraycopy(array2, 0, mergedArray, length1, length2);
// Print the merged array
System.out.println("Merged Array: " + Arrays.toString(mergedArray));
}
}
Output
Method 2. Without Using Built-in Functions
You can also merge two arrays manually by iterating through each array and copying their elements into a new array.
Code example
import java.util.Arrays;
public class MergeArraysManually {
public static void main(String[] args) {
int[] array1 = {10, 20, 30};
int[] array2 = {40, 50, 60};
// Create a new array to hold the merged result
int[] mergedArray = new int[array1.length + array2.length];
// Copy elements from the first array
for (int i = 0; i < array1.length; i++) {
mergedArray[i] = array1[i];
}
// Copy elements from the second array
for (int i = 0; i < array2.length; i++) {
mergedArray[array1.length + i] = array2[i];
}
// Print the merged array
System.out.println("Merged Array: " + Arrays.toString(mergedArray));
}
}
Output
Method 3. Using Java Streams
If you're using Java 8 or later, you can leverage streams to merge arrays in a more functional style.
Code example
import java.util.Arrays;
import java.util.stream.IntStream;
public class MergeArraysUsingStreams {
public static void main(String[] args) {
int[] array1 = {10, 20, 30};
int[] array2 = {40, 50, 60};
// Merge arrays using Java Streams
int[] mergedArray = IntStream.concat(Arrays.stream(array1), Arrays.stream(array2)).toArray();
// Print the merged array
System.out.println("Merged Array: " + Arrays.toString(mergedArray));
}
}
Output
Method 4. Using ArrayList
Another approach is to use an ArrayList to facilitate merging two arrays. This method is particularly useful when dealing with dynamic sizes.
Code Example
import java.util.ArrayList;
import java.util.Arrays;
public class MergeArraysUsingArrayList {
public static void main(String[] args) {
Integer[] array1 = {10, 20, 30};
Integer[] array2 = {40, 50, 60};
// Create an ArrayList and add elements from both arrays
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array1));
list.addAll(Arrays.asList(array2));
// Convert back to an array if needed
Integer[] mergedArray = list.toArray(new Integer[0]);
// Print the merged array
System.out.println("Merged Array: " + Arrays.toString(mergedArray));
}
}
Output
Conclusion
In this article, we explored various methods for merging two arrays in Java. From using System.arraycopy() for efficiency to leveraging Java Streams and ArrayList for flexibility and ease of use—each method has its own advantages. Depending on your specific needs and the context in which you're working with arrays in Java, you can choose the most suitable approach.