Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic