0

I am trying to merge two different arrays into one merged Array. I have tried ArrayList but then I get an error stating that the numbers are used but never called.

Below is the code I have so far.

public class main {

     static class Solution {
        public void findLength(int[] A, int[] B) {

            
            
        }

    }

    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);


    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Jose Beltran
  • 57
  • 1
  • 6
  • 1
    What does *merge* exactly mean here? Do you want the result to be sorted or containing possible duplicates? Or not? – deHaar Sep 03 '20 at 07:53
  • Does this answer your question? [How can I concatenate two arrays in Java?](https://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) – Hulk Sep 03 '20 at 07:54
  • 1
    To find the length, it is not necessary to merge the arrays. Just calculate the sum of the individual lengths. – Henry Sep 03 '20 at 07:54
  • So there is not a way to merge both array 1 and array 2 together into one who array? – Jose Beltran Sep 03 '20 at 07:56
  • Looks duplicated from: https://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java – Brother Sep 03 '20 at 08:00
  • Can you please not use the [intellij-idea] tag unless your question is specifically about Intellij. This question is nothing to do with your IDE. Mistagging question will cause people who use tags correctly when search and filtering to waste their time on questions that are not relevant to them. I have fixed this Question's tags, but I notice that you have done the same thing on other Questions. Please fix them. – Stephen C Sep 03 '20 at 08:17

2 Answers2

1

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
Hulk
  • 6,399
  • 1
  • 30
  • 52
Awais ali
  • 109
  • 6
  • While the second loop works, it's not very self-explanatory. I rather suggest one similar to the A-loop, just running through B this time: `for(int i=0;i – tevemadar Sep 03 '20 at 08:25
0

you can just copy the arrays as like you were merging them

int[] row1 = {1,2,3,4,5,6};
int[] row2 = {7,8,9,10,11,12};
int x1 = row1.length;

int x2 = row2.length;

int[] result = new int[x1 + x2];

System.arraycopy(row1, 0, result, 0, x1);  
System.arraycopy(row2, 0, result, x1, x2);  
System.out.println(Arrays.toString(result));
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97