• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Code Help

 
Johann Ponderghast
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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.



 
Jason Irwin
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic