Google Guava is a java library with lot of utilities and reusable components.
This requires the library guava-10.0.jar to be in classpath.
The following example shows using Ints.indexOf() and
Ints.lastIndexOf() API.
It returns the index of first and last matching element from the given integer array.
/**
* @param args
*/ public static void main(String[] args) { int [] values = { 1, 2, 4, 2, 1, 2, 4, 2}; int value2Find = 2;
System.out.println("The first index of '" + value2Find +
"' in the array : " + Ints.indexOf(values, value2Find));
System.out.println("The last index of '" + value2Find +
"' in the array : " + Ints.lastIndexOf(values, value2Find));
value2Find = 6;
System.out.println("The first index of '" + value2Find +
"' in the array : " + Ints.indexOf(values, value2Find));
System.out.println("The last index of '" + value2Find +
"' in the array : " + Ints.lastIndexOf(values, value2Find));
value2Find = 4;
System.out.println("The first index of '" + value2Find +
"' in the array : " + Ints.indexOf(values, value2Find));
System.out.println("The last index of '" + value2Find +
"' in the array : " + Ints.lastIndexOf(values, value2Find));
}
}
It gives the following output,
The first index of '2' in the array : 1
The last index of '2' in the array : 7
The first index of '6' in the array : -1
The last index of '6' in the array : -1
The first index of '4' in the array : 2
The last index of '4' in the array : 6