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)?
Asked
Active
Viewed 408 times
0

Lucas Baizer
- 305
- 2
- 5
- 13
2 Answers
3
You can create List wrapping your array and use its indexOf
method
Arrays.asList(yourArray).indexOf(element)

Pshemo
- 122,468
- 25
- 185
- 269
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;
}

singhakash
- 7,891
- 6
- 31
- 65