Results 1 to 3 of 3
Thread: Array index from element?
- 04-07-2012, 08:38 PM #1
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
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:
Can I do something like... trees.index("Maple") to get the index, in this case 0?Java Code:String[] trees = {"Maple", "Pine", "Burch"}
- 04-07-2012, 09:22 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-07-2012, 09:31 PM #3
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Pick random array index and change the element value
By Space Ranger in forum New To JavaReplies: 8Last Post: 03-29-2011, 10:18 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
Max element in an Array
By mew in forum New To JavaReplies: 5Last Post: 12-03-2007, 05:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks