0

I have an array like so: String[] articles = {"a", "and", "the"}; And I want to know how to get the array index from a certain term. Is there a method along the lines of articles.getIndex("the"); and it will return an int (in this case, 2)?

0

2 Answers 2

3

You can create List wrapping your array and use its indexOf method

Arrays.asList(yourArray).indexOf(element)
Sign up to request clarification or add additional context in comments.

Comments

0

You can also write method for that

public int indexByValue(String data, String[] array) {
    int index = -1;
    for (int i = 0; i < array.length; i++)
      if (array[i].equals(data)) index = i;

    return index;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.