Array index from element?
Is it possible to get the index number from an element in that array? I was trying to make a numbered list with a for loop using the array index instead of a seperate incrementing variable.
For example:
Code:
String[] trees = {"Maple", "Pine", "Burch"}
Can I do something like... trees.index("Maple") to get the index, in this case 0?
Re: Array index from element?
You can iterate over the array in a for loop, and test each element in turn for equality. When the equality test passes, the value of the loop index is what you want.
Alternatively, you can use a java.util.List instead of an array. List has a method that can serve your purpose.
db
Re: Array index from element?
Thanks, I'll give that a try.