• Post Reply Bookmark Topic Watch Topic
  • New Topic

index out of bounds  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a class that sorts and matches items in a collection of arrays. In the end, elements common to all 3 arrays should be printed. The idea is that as the first array is compared to the second it stores the matched items in tempArray. When all items are compared the tempArray should overwrite the checked array, and the process continues until all arrays are checked. I have set all the checking in a do loop that should run while the value is <= to the array length. This allows all items in the reference array to be checked. I get an index out of bounds message but not where I woudl expect it. I have tried varying the condition in the do while loop in the match method, but it did not change the result. There may be other issues I have not addressed with solving the algorithm, but this one has me stumped and I am not able to progress.




(Bartender cleanup: added code tags)
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Printing out the stack trace would likely help here. Especially, since the exception would also tell you which array index went out of bounds, and by how much.

Henry
 
mark cortez
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Printing out the stack trace would likely help here. Especially, since the exception would also tell you which array index went out of bounds, and by how much.

Henry


I inserted the following

exception handling was only briefly covered so I am not sure if I wrote the try catch block correctly or what I woudl be looking for if I did.

(Bartender cleanup: fixed code tags)
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Henry meant was that you should show us the stack trace so we can help you identify the problem.

However I notice that in one of your while loops you're looping until k <= collections[i].length, and in the while loop you access collections[i][k].

Well remember that array indices are zero-based. An array with a length of 5 has indices 0-4. Given an array x, x[x.length] will always be out of bounds.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, please UseCodeTags (←click that link) when you post code.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't catch the exception; you should fix the bug that is causing it.
 
Bartender
Posts: 1838
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that the values of i,j,k and l can change DURING your loop (they are effectively global variables).

So even if you START with ok values, you might have values out of bounds halfway through.

Kudos to the OP for actually having a compilable example. Running it produces this exception :
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!