[Solved] My Java code is giving me an out of bounds error


  1. Out-of-bound errors means you’re probably not accessing indices right, so check which of your data structures are vulnerable to that.
  2. Study the error message you get because that at least shows the line where the error happens (would have been better if it was included in the post).

It seems the second for-loop in the fun method is causing the error:

for(int h = 0; h < nums2.length; h++) {
    b.add(nums1[h]);
}

Why is nums1 being referenced instead of nums2?

4

solved My Java code is giving me an out of bounds error