• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Code Help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just cannot--for the life of me--figure out what is wrong with this code. Basically what I'm trying to do is take a value and take the index every time that value comes up in the first array. And I make a new array with the list of indexes. I don't have syntax errors, but I cannot figure out what is wrong with it, as I said before. And thanks in advance for the help.



 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code as shown will not compile, "a" and "findAllArray" are not defined.

e is never incremented, it is always -1; that will give you an ArrayIndexOutOfBoundsException on your first value match.

Another problem you have is that you do not know how many results you will have, so you can't really use something fixed-length like a basic array. What if you have more results that you anticipate? You'll have an ArrayIndexOutOfBoundsException again. What if you run this method twice? After the second run you could have values left behind from the first and that if probably not good.

There are few ways to solve this. You could use a Set, a List or even a StringBuilder (use a delimiter, then do a toString and split, remembering your result will be a String array). Personally I'd use a List instance.

Why is the return just an int (in this case the j-th value in findAllArray)? Did you not say you wanted to return an array value-match indexes?
 
reply
    Bookmark Topic Watch Topic
  • New Topic