• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

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

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, that was it. Thanks a ton, and especially for the quick reply.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic