Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't understand why my array index is out of bounds  RSS feed

 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all. Happy Thanksgiving!

I am running up against an ArrayIndexOutOfBoundsException I don't understand. Here's the code:


What this is supposed to do is take elements that are duplicates of another element in the array, and put it into a separate array, and make a word count for each unique word. Right now it seems to build the wrong array, it will put the first element in the right spot and so on until the fourth element, which is (incorrectly) a duplicate. So the array should have three elements in it under the test conditions I am running, and then the program should proceed. But it's continuing to put elements in even though they are duplicates.

Here's the code for my equals method:


Which all looks right to me.

When I comment out the line in question, the rest of the program runs fine until I need that particular piece of information, but I am at a loss as to what is happening inside that loop. Any thoughts?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two loops. Both looping from 0 to words.length. So what's the biggest value that indx can become ? words.length*words.length. Which is more than uniqueWords' max size.
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I thought that indx would only increment if a unique word was found, in other words, if the two satisfying conditions are met from the if statement:

Is that being bypassed somehow?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't it be indexOfDuplicate[j] != 1 then ? (instead of indexOfDuplicate[i] != 1)
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that was it. Thanks a ton, and especially for the quick reply.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!