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 331 times
0
Lucas Baizer
- 295
- 2
- 4
- 13
2 Answers
3
You can create List wrapping your array and use its indexOf method
Arrays.asList(yourArray).indexOf(element)
Pshemo
- 118,400
- 24
- 176
- 257
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,773
- 5
- 28
- 61