-1

I am trying to merge 2 Ascending sort array in java. I am new at java so I can not understand. What is the problem with the following code?

import java.lang.Math;
import java.util.Scanner;

public class Main
{   
  public static void main(String[] args) {
    int[] a = {10, 20, 30, 40};
    int[] b = {50, 60, 70, 80};
    int[] z = merge(a, b);
    System.out.println(z);
  }

  public static int[] merge(int[] a, int[] b) {
    int[] result = new int[a.length + b.length];
    int indexResult = 0;
    int indexA = 0;
    int indexB = 0;

    while (indexResult < result.length) {
      if (indexB >= a.length || a[indexA] <= b[indexB])
        result[indexResult++] = b[indexB++];
      else
        result[indexResult++] = a[indexA++]; 
    }
    return result;
   }
 }

My error is

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4                                                                         
        at Main.merge(Main.java:30)                                                                                                            
        at Main.main(Main.java:15)
j3ff
  • 5,719
  • 8
  • 38
  • 51
  • `indexB >= a.length` should be replaced with `indexA >= a.length` which is the reason for `ArrayIndexOutOfBoundsException` – backdoor Apr 18 '20 at 19:14
  • This Question inspired me to ask about specifically asking if Java Streams could do this work. Indeed, it can be done with streams. See: [*Using Java streams to merge a pair of `int` arrays*](https://stackoverflow.com/q/61297074/642706). – Basil Bourque Apr 18 '20 at 22:36

2 Answers2

0

indexB >= a.length should be replaced with indexA >= a.length.

Following code may help

import java.lang.Math;
import java.util.Scanner;
public class Main
{   
public static void main(String[] args) {
     int[] a = {10, 20, 30, 40};
    int[] b ={50,60,70,80};
    int[] z=merge( a, b);
    for(int x: z)
        System.out.println(x);

}
public static int[] merge(int[] a, int[] b) {

        int[] result = new int[a.length + b.length];
        int indexResult = 0;
        int indexA = 0;
        int indexB = 0;

        while (indexResult < result.length) {
            if (indexA >= a.length || a[indexA] >= b[indexB])
                result[indexResult++] = b[indexB++];
            else
                result[indexResult++] = a[indexA++];
        }
        return result;
    }
}

OUTPUT:-

10
20
30
40
50
60
70
80
backdoor
  • 891
  • 1
  • 6
  • 18
  • thanks it worked what what if we have int[] b = {10, 20, 30, 40}; and int[] a = {50, 60, 70, 80}; this code won't work. – mourcheh joon Apr 19 '20 at 06:01
0

There are many different ways to merge 2 arrays.This is another example:

  public class Main {
    public static void main(String[] args) {
        int[] a = { 10, 20, 30, 40 };
        int[] b = { 50, 60, 70, 80 };
        merge(a, b);

    }

    public static void merge(int[] a, int[] b) {

        int[] z = new int[a.length + b.length];
        int count = 0;

        for (int i = 0; i < a.length; i++) {
            z[i] = a[i];
            count++;
        }
        for (int j = 0; j < b.length; j++) {
            z[count++] = b[j];
        }
        for (int i = 0; i < z.length; i++)
            System.out.print(z[i] + " ");
    }

}

Output:

10 20 30 40 50 60 70 80 
Cleo
  • 136
  • 1
  • 10