7

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.

3
  • 1
    Unclear, do you want the index of the String or do you want the String matching the english name of the number? Commented Oct 1, 2015 at 9:11
  • @Tunaki Get element by index vs get index by element :) Commented Oct 1, 2015 at 9:15
  • 1
    The List api is here Commented Oct 1, 2015 at 9:15

1 Answer 1

8

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");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.