Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Find the index of a String in a list

Tags:

java

list

I have a list:

public static  List<String> codes = new LinkedList<String>(Arrays.asList(
            "Zero",
            "One"
            )); //Etc..

I want to be able to find the number of a String's position when I type it. For instance, if I type One, I want the program to print out 1. Currently, I can only do the reverse by using codes.get();

Correction: I want the program to print out the position of the String in the list. Zero has the position 0, if I do codes.get(0); I get Zero. But how do I do the reverse?

Is there a way to smoothly do this? Thanks in advance.

like image 409
Ainvox Avatar asked Oct 01 '15 09:10

Ainvox


1 Answers

The reverse of codes.get() is indexOf(Object obj) method

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

codes.indexOf("One");
like image 142
Suresh Atta Avatar answered Oct 11 '22 00:10

Suresh Atta