int[] arrFirst = {1,6}; int[] arrSecond = {4}; public void addition() { int[] arr = new int[this.arrFirst.length+this.arrSecond.length]; for (int i = 0, j = 0, m = 0; i != arr.length; i++) { if (this.arrFirst[m] < this.arrSecond[j]) { arr[i] = this.arrFirst[m]; if (m != this.arrFirst.length){ m++; } System.out.println(arr[i]); } else { arr[i] = this.arrSecond[j]; if (j < this.arrSecond.length){ j++; } System.out.println(arr[i]); } } 

The problem is that I have ArrayIndexOutOfBoundsException: 1 when checking this.arrFirst[m] < this.arrSecond[j] at the third iteration, although j that follows this is set to if (j != this.arrSecond.length) and increment at Access to valley -1 array should not.

Tell me where I screwed up? Something is not like I will not find (((

  • in the previous question there is an example of code, if you compare it with your own, you will see that you do not have checks that you have shoved everything into one array - Grundy
  • Well, if you have m already equal to length - then on the line above you will get an exception, which you will get now :) - Grundy

2 answers 2

the problem was that j getting bigger than arrSecond.length .

 public static void main(String[] args) { int[] arrFirst = {1,6, 7, 8}; int[] arrSecond = {2,4}; int[] arr = new int[arrFirst.length+arrSecond.length]; for (int i = 0, j = 0, m = 0; i != arr.length ; i++) { if(m < arrFirst.length) { if (j == arrSecond.length) { arr[i] = arrFirst[m]; m++; System.out.println(arr[i]); continue; } if (arrFirst[m] < arrSecond[j]) { arr[i] = arrFirst[m]; if (m != arrFirst.length) { m++; } System.out.println(arr[i]); } else { arr[i] = arrSecond[j]; if (j != arrSecond.length) { j++; } System.out.println(arr[i]); } } } 

conclusion

 1 2 4 6 7 8 

UPD

answer to the question from the comment of this answer

how j got bigger than arrSecond.length!

1 iteration:

 i = 0, j = 0, m = 0 

if (j == arrSecond.length) condition is satisfied, where m++

2 iteration:

 i = 1, j = 0, m = 1 

the condition is not satisfied -> go to else, where j++

3rd iteration: i = 2, j = 1, m = 1

you have j responsible for the second array, which has 1 element, so arrSecond[j] will give you an ArrayIndexOutOfBoundsException , because you have gone beyond the array.

  • only this code does not sort the array in ascending order - Senior Pomidor
  • Yes, but how did j get bigger than arrSecond.length! That's what is not clear because it is indicated that it increases only if if (j! = This.arrSecond.length). Nowhere else does its value change, it is only used. That's what's not clear. The fact that it is getting bigger is what I understand, and so why? This is what I don't understand ... - Pavel
  • @Pavel, the array indexing starts from 0. when m = length-1, the check passes, m increases, on the next lap everything drops, because the array cannot contain an element with the index number equal to the length - Grundy
  • @Pavel updated the answer - Senior Pomidor Nov.
  • Thank you very much for the answer! Now it is. - Pavel Nov.

Try here if (j != this.arrSecond.length) to replace != with <

  • No, unfortunately it did not work. - Pavel Nov.