jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Array Code Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Code Help" Watch "Array Code Help" New topic

Array Code Help

Johann Ponderghast

Joined: May 01, 2009
Posts: 3
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

Joined: Jun 09, 2009
Posts: 327
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?

I agree. Here's the link: http://aspose.com/file-tools
subject: Array Code Help
It's not a secret anymore!