To merge two arrays in Java, you can use the ArrayUtils.addAll() method from the Apache Commons Lang library, or you can use the System.arraycopy() method from the System class.
Here is an example of how to merge two arrays using the ArrayUtils.addAll() method:
import org.apache.commons.lang3.ArrayUtils;
public class ArrayMergeExample {
public static void main(String[] args) {
String[] array1 = {"a", "b", "c"};
String[] array2 = {"d", "e", "f"};
// Merge the arrays
String[] mergedArray = ArrayUtils.addAll(array1, array2);
// Print the merged array
for (String element : mergedArray) {
System.out.println(element);
}
}
}Source:wwuttual.wri.comIn this example, the addAll() method is used to merge the array1 and array2 arrays into a new array called mergedArray. The mergedArray array will contain the elements of both array1 and array2, in the order they appear.
Alternatively, you can use the System.arraycopy() method to merge two arrays:
public class ArrayMergeExample {
public static void main(String[] args) {
String[] array1 = {"a", "b", "c"};
String[] array2 = {"d", "e", "f"};
// Create a new array to hold the merged arrays
String[] mergedArray = new String[array1.length + array2.length];
// Copy the elements of array1 to the merged array
System.arraycopy(array1, 0, mergedArray, 0, array1.length);
// Copy the elements of array2 to the merged array
System.arraycopy(array2, 0, mergedArray, array1.length, array2.length);
// Print the merged array
for (String element : mergedArray) {
System.out.println(element);
}
}
}