0

I have two sorted arrays. I need to connect both of them into one new sorted array:

    int[] arr1 = {1,2,3,6,8};
    int[] arr2 = {4,5,9,12,208,234};
    printArr(allSort(arr2,arr1));
}

public static int[] allSort(int[] arr, int[] arr3) {

    int[] newArr = new int[arr.length + arr3.length];

    int j = 0;
    int k = 0;

    for (int i = 0; i < newArr.length - 1; i++) {
        if(j == arr3.length){
            newArr[i] = arr[k];
            k++;
        }
        if(k == arr.length){
            newArr[i] = arr3[j];
            j++;
        }
        if(arr[k] > arr3[j]){
            newArr[i] = arr3[j];
            j++;
        } else if (arr[k] < arr3[j]) {
            newArr[i] = arr[k];
            k++;
        }
    }
    return newArr;
}

I tried to build an array that has a length equal to the length of the both arrays summed together and then run a loop on it.

However, this code returns the error: AArrayIndexOutOfBoundsException: 5.

Matthew Anderson
  • 338
  • 1
  • 13
Sufferring
  • 49
  • 5

3 Answers3

0

Just add continue in both the if condition like this,

if(j == arr3.length){
  newArr[i] = arr[k];
  k++;
  continue;
}
if(k == arr.length){
  newArr[i] = arr3[j];
  j++;
  continue;
}

So here anyway the other loop is completed thats why we are iterating and adding all the values so it doesn't need to check all other conditions so we can skip it.

Also,

for (int i = 0; **i < newArr.length**; i++)

Since you are checking "<".

Arun Prasat
  • 340
  • 1
  • 9
  • i tried your first solution and it didnt change the problem. My teacher told me not to use any thing we have not learnt yet. and we did not learn about continue. so i cant use it. – Sufferring Jun 30 '19 at 01:32
  • I have edited now please try this it will work – Arun Prasat Jun 30 '19 at 01:33
  • the continue suggestion was kinda useful but i cant use it... i tried to change the loop it still array index ... : 5. – Sufferring Jun 30 '19 at 01:36
  • try this, **boolean check = false; for (int i = 0; i < newArr.length; i++) { if(j == arr3.length && k != arr.length){ newArr[i] = arr[k]; k++; check = true; } if(k == arr.length && j != arr3.length){ newArr[i] = arr3[j]; j++; check = true; } if(!check && arr[k] > arr3[j]){ newArr[i] = arr3[j]; j++; } else if (!check && arr[k] < arr3[j]) { newArr[i] = arr[k]; k++; } }** – Arun Prasat Jun 30 '19 at 01:38
  • it works, but its missing two ints, the last two numbers in arr2 (208 and 234) plus i would love a short explanation... and take me to your way of thinking please :) – Sufferring Jun 30 '19 at 01:49
  • actually you need to remove the "-1" in for (int i = 0; **i < newArr.length**; i++), Explanation, here what I did is if it went into any of the first two if condition it means one of the array is reached its end so we don't need to check each other. Thats why i added a check to ignore the sorting condition. – Arun Prasat Jun 30 '19 at 01:53
  • Oh , okay :) thank you very much. But why is it missing two ints? – Sufferring Jun 30 '19 at 01:55
  • int[] newArr = new int[arr.length + arr3.length]; This should be like this or the size will be reduced and it cant accommodate the last two array items. I am happy that I helped you :) please accept the answer – Arun Prasat Jun 30 '19 at 01:58
0

The ArrayIndexOutOfBoundsException() is an Exception and what it basically means is that at some point you're trying to access a element of an array with an illegal index. Refer to the ArrayIndexOutOfBoundsException Documentation for more informations.

after looking to your code at some point here are the index's values:

enter image description here.

In the loop you're calling arr[k] with k = 5 in if(arr[k] > arr3[j]) as arr is an Array of length 5 and thus has a maximum index of 4 and that is why you're getting an out of bounds exception.

Kil jeaden
  • 93
  • 7
  • How can I eliminate k being = 5? I tried ifs to check if it 5 or not, but I didn't work – Sufferring Jun 30 '19 at 01:54
  • @Sufferring You can simply add an if statement and in the loop like if(k<5), you'll notice that your method runs correctly without throwing any exception, however you're not going to get the correct result, the problem is that your code is wrong. You're doing something wrong in the allSort method. – Kil jeaden Jun 30 '19 at 02:04
0

Your main problem is control when first array finished.

I made some adjusts on your code and now it's working.

public static void main(String[] args) {
    int[] arr1 = { 1, 2, 3, 6, 8 };
    int[] arr2 = { 4, 5, 9, 12, 208, 234 };

    int[] newArr = allSort(arr1, arr2);

    for (int i = 0; i <= newArr.length - 1; i++) {
        System.out.println(" " + newArr[i]);
    }
}

public static int[] allSort(int[] arr1, int[] arr2) {
    int j = 0;
    int k = 0;
    boolean endArr1 = false;
    int[] newArr = new int[arr1.length + arr2.length];

    for (int i = 0; i <= newArr.length - 1; i++) {
        if (arr1[k] < arr2[j] && !endArr1) {
            System.out.println("k: " + k + " " + arr1.length);
            newArr[i] = arr1[k];
            if(k < arr1.length-1)
                k++;
            else
                endArr1 = true;
       } else if (arr2[j] < arr1[k]  || endArr1) {
           System.out.println("j: " + j + " " + arr2.length);
           newArr[i] = arr2[j];
           if(j < arr2.length-1)
               j++;
         }
    }
    return newArr;
}
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58