This is how you can merge two arrays.
static class Solution {
public int findLength(int[] A, int[] B) {
return A.length+B.length;
}
public int[] getMergedArray(int[] A, int[] B){
int arrayLength=this.findLength(A, B);
int mergedArray[]= new int[arrayLength];
//copying first array data
for (int i = 0; i < A.length; i++) {
mergedArray[i]=A[i];
}
for (int i = A.length; i < arrayLength; i++) {
mergedArray[i]=B[i-A.length];
}
return mergedArray;
}
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] row1 = {1,2,3,4,5,6};
int[] row2 = {7,8,9,10,11,12};
solution.findLength(row1,row2);
System.out.println("First Array");
for (int i = 0; i < row1.length; i++) {
System.out.println(" "+row1[i]);
}
System.out.println("Second Array ");
for (int i = 0; i < row2.length; i++) {
System.out.println(" "+row2[i]);
}
System.out.println("Merged Array ");
int mergedArray[]=solution.getMergedArray(row1, row2);
for (int i = 0; i < mergedArray.length; i++) {
System.out.println(" "+mergedArray[i]);
}
}
Output
run:
First Array
1
2
3
4
5
6
Second Array
7
8
9
10
11
12
Merged Array
1
2
3
4
5
6
7
8
9
10
11
12