0

I have written code to merge two sorted arrays in Java.

public class MergeSortedArrays {
    public static int[] my_array     = {3, 4, 6, 10, 11, 15};
    public  static int[] alices_array = {1, 5, 8, 12, 14, 19};
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int length = my_array.length + alices_array.length;

        int[] new_array = new int[length];
        int i = 0;
        int j = 0;
        int count = 0;

        while (count < length) {

            if(my_array[i] < alices_array[j] ) {
                new_array[count] = my_array[i];
                i++;
            } else {
                new_array[count] = alices_array[j];
                j++;
            }

            count++;
        }
        for(int k=0;k<length;k++) {
            System.out.println(new_array[k]);
        }
    }

}

But I am getting this error-

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at MergeSortedArrays.main(MergeSortedArrays.java:15)

One array is exhausted before merging is completed. How can I handle this case?

durron597
  • 31,968
  • 17
  • 99
  • 158
Newbie
  • 2,664
  • 7
  • 34
  • 75

3 Answers3

1

You can't check if my_array[i] < alices_array[j] if you're past the end of my_array. So ensure that i is valid; if it's not, add an element from j. Similarly, you don't want to check j if it's past the end of that array. So, use this check instead of your previous if statement:

if(j >= alices_array.length || 
       (i < my_array.length && my_array[i] < alices_array[j] )) {

The reason it doesn't attempt to evaluate my_array[i] is because Java doesn't need to evaluate the second half of an && expression if the first half is false. See Does Java evaluate remaining conditions after boolean result is known?


This answer will help you to solve this problem, but you should really read this article for getting better at programming:

Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158
1

The problem is, that when one array is done, you should only consider the other one:

while (count < length) {
    if (i == my_array.length) {
        new_array[count] = alices_array[j];
        j++;
    } else if (j == alices_array.length) {
        new_array[count] = my_array[i];
        i++;
    } else {
        if(my_array[i] < alices_array[j] ) {
            new_array[count] = my_array[i];
            i++;
        } else {
            new_array[count] = alices_array[j];
            j++;
        }
    }
    count++;
}
dosw
  • 431
  • 2
  • 10
  • There's no need to copy the other array by hand once one array is exhausted. Use `System.arraycopy` (or its wrappers in `Arrays`) which is implemented in native code and way more efficient. – billc.cn Aug 12 '15 at 15:25
0

You must check the range of i and j in case the reach the end.

        if (i < my_array.length) {
            if (j < alices_array.length) {
                if (my_array[i] < alices_array[j]) {
                    new_array[count] = my_array[i];
                    i++;
                } else {
                    new_array[count] = alices_array[j];
                    j++;
                }

            } else {
                new_array[count] = my_array[i];
                i++;
            }
        } else {
            if (j < alices_array.length) {
                new_array[count] = alices_array[j];
                i++;
            }
        }
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213