# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Merge Two Arrays in Java | Array Programs in Java – 10 | In the previous Java program, we have seen different ways to copy arrays in Java, which also will be used in this program. Now in this post, we will discuss how to merge two arrays in Java.
How to merge two arrays in Java? Merging of two arrays in Java also can be done by using pre-defined methods, or we can do it manually by using loops. Let us discuss them one by one.
Example of merging of two int arrays,
Array1 = [10, 20, 30, 40, 50]
Array2 = [9, 18, 27, 36, 45]
Then the Result should be,
Merged Array = [10, 20, 30, 40, 50, 9, 18, 27, 36, 45]
Example of merging of two String array,
Array1 = [Java, Python, C++]
Array2 = [HTML, CSS, JavaScript]
Then the Result should be,
Merged Array = [Java, Python, C++, HTML, CSS, JavaScript]
Merge Two Arrays in Java using Loops
Steps to combine two arrays in Java,
a) Take two array which will be merged, assume src1 and src2.
b) Declare a new array with the size of both array (src1.length + src2.length ).
c) Copy first array (src1) to new array from 0 to src1.length-1
d) Copy second array (src2) to new array from src1.length to (src1.length + src2.length).
Now, let us demonstrate it through an example. Merge two arrays without using a pre-defined method.
Program to merge two arrays in Java using loops
import java.util.Arrays;
public class CopyArray {
public static void main(String[] args) {
// array which should be merged
int src1[] = {10, 20, 30, 40, 50};
int src2[] = {9, 18, 27, 36, 45};
// create new array
int newArray[] = new int[src1.length + src2.length];
// Copy first to new array from 0 to src1.length
for(int i=0; i<src1.length; i++) {
newArray[i] = src1[i];
}
// copy second array to new array
for(int i=0, j=src1.length; j<(src1.length + src2.length);
j++, i++) {
newArray[j] = src2[i];
}
// display all array
System.out.println("Array1 = " + Arrays.toString(src1));
System.out.println("Array2 = " + Arrays.toString(src2));
System.out.println("Merged Array = "
+ Arrays.toString(newArray));
}
}
Output:-
Array1 = [10, 20, 30, 40, 50]
Array2 = [9, 18, 27, 36, 45]
Merged Array = [10, 20, 30, 40, 50, 9, 18, 27, 36, 45]
In this program, to display the array we have used the toString() method of java.util.Arrays class given to convert array to String type.
Merge Two Array using System.arraycopy() method
In place of loops, we can also use the pre-defined method System.arraycopy(). The System.arraycopy() method in Java is given to copy an array to another array. It copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
Syntax of the arraycopy() method in java.lang.System class:- public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
Parameters in this method:-
- src:- The source array.
- srcPos:- Starting position in the source array.
- dest:- The destination array.
- destPos:- starting position in the destination array.
- length:- the number of array elements to be copied.
Since java.lang.System class is imported by default in all Java classes therefore to use the arraycopy() method no need to explicitly import them.
Program to merge two array using System.arraycopy() method
import java.util.Arrays;
public class CopyArray {
public static void main(String[] args) {
// array which should be merged
int src1[] = {10, 20, 30, 40, 50};
int src2[] = {9, 18, 27, 36, 45};
// create new array
int newArray[] = new int[src1.length + src2.length];
// Copy first to new array from 0 to src1.length
System.arraycopy(src1, 0, newArray, 0, src1.length);
// copy second array to new array
System.arraycopy(src2, 0, newArray, src1.length, src2.length);
// display all array
System.out.println("Array1 = " + Arrays.toString(src1));
System.out.println("Array2 = " + Arrays.toString(src2));
System.out.println("Merged Array = "
+ Arrays.toString(newArray));
}
}
Output:-
Array1 = [10, 20, 30, 40, 50]
Array2 = [9, 18, 27, 36, 45]
Merged Array = [10, 20, 30, 40, 50, 9, 18, 27, 36, 45]
How to Merge Two String Arrays in Java
In the previous examples, we were merging the int array. Now, let us see those programs by merging two String arrays.
import java.util.Arrays;
public class CopyArray {
public static void main(String[] args) {
// array which should be merged
String src1[] = {"Java", "Python", "C++"};
String src2[] = {"HTML", "CSS", "JavaScript"};
// create new array
String newArray[] = new String[src1.length + src2.length];
// Copy first to new array from 0 to src1.length
System.arraycopy(src1, 0, newArray, 0, src1.length);
// copy second array to new array
System.arraycopy(src2, 0, newArray, src1.length, src2.length);
// display all array
System.out.println("Array1 = " + Arrays.toString(src1));
System.out.println("Array2 = " + Arrays.toString(src2));
System.out.println("Merged Array = "
+ Arrays.toString(newArray));
}
}
Output:-
Array1 = [Java, Python, C++]
Array2 = [HTML, CSS, JavaScript]
Merged Array = [Java, Python, C++, HTML, CSS, JavaScript]
Merge Two Arrays Using Stream
In the case of an int array, we can use the concat() method of IntStream which takes two IntStreams. To convert an array to IntStream we can use IntStream.of() method.
import java.util.Arrays;
import java.util.stream.IntStream;
public class Test {
public static void main(String[] args) {
int[] array1 = { 10, 20, 30, 40, 50 };
int[] array2 = { 9, 18, 27, 36, 45 };
int[] mergedArray = IntStream
.concat(IntStream.of(array1), IntStream.of(array2))
.toArray();
System.out.println(Arrays.toString(mergedArray));
}
}
Or, we can use Arrays.stream() method to convert an array to IntStream as follows:-
int[] mergedArray = IntStream
.concat(Arrays.stream(array1), Arrays.stream(array2))
.toArray();
In the case of String arrays, we can use Stream.concat() method:-
import java.util.Arrays;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
String[] src1 = { "Java", "Python", "C++" };
String[] src2 = { "HTML", "CSS", "JavaScript" };
String[] mergedArray = Stream
.concat(Arrays.stream(src1), Arrays.stream(src2))
.toArray(String[]::new);
System.out.println(Arrays.toString(mergedArray));
}
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!