This example shows how to find an element in a Vector using the indexOf and lastIndexOf methods. This example also shows how to find the element index in the Vector of custom class objects.
How to find the element index in Vector in Java?
How to find the index of the first occurrence of an element using the indexOf method?
The Vector indexOf
method returns the index of the first occurrence of the specified element within the vector object.
1 |
public int indexOf(Object element) |
It returns -1 if the specified element is not found in the vector object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.Vector; public class VectorFindElementIndexExample { public static void main(String[] args) { Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); /* * To find element index in the vector, use * the indexOf method */ /* * This will return 1 i.e. index of the first * occurrence of the element 20 */ System.out.println( vNumbers.indexOf(20) ); //this will return -1 as element 40 does not exist System.out.println( vNumbers.indexOf(40) ); } } |
Output
1 2 |
1 -1 |
The overloaded indexOf
method returns the index of the first occurrence of the given element starting from the specified index of the vector object.
1 |
public int indexOf(Object element, int startIndex) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); /* * This will return 4 i.e. index of the first * occurrence of the element 20 starting from index 2 */ System.out.println( vNumbers.indexOf(20, 2) ); /* * this will return -1 as the element 20 does not exist * at or after index 5 */ System.out.println( vNumbers.indexOf(20, 5) ); |
Output
1 2 |
4 -1 |
How to find the index of the last occurrence of an element using the lastIndexOf method?
The Vector lastIndexOf
method returns the index of the last occurrence of the specified element in the vector object.
1 |
public int lastIndexOf(Object element) |
It returns -1 if the element is not found in the vector object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); /* * This will return 4 i.e. index of the last * occurrence of the element 20 */ System.out.println( vNumbers.lastIndexOf(20) ); /* * this will return -1 as the element 40 does not exist */ System.out.println( vNumbers.lastIndexOf(40) ); |
Output
1 2 |
4 -1 |
The overloaded lastIndexOf
method returns the last index of the specified element starting from the given index searching backward.
1 |
public int lastIndexOf(Object element, int startIndex) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); /* * This will return 4 i.e. index of the last * occurrence of the element 20 from index 5 * searching backwards */ System.out.println( vNumbers.lastIndexOf(20, 5) ); /* * This will return 1 i.e. index of the last * occurrence of the element 20 from index 3 * searching backwards */ System.out.println( vNumbers.lastIndexOf(20, 3) ); /* * This will return -1 as the element 30 * cannot be found starting index 1 and searching * backwards */ System.out.println( vNumbers.lastIndexOf(30, 1) ); |
Output
1 2 3 |
4 1 -1 |
How to find an index of a custom class object in the Vector?
If the Vector elements are objects of a custom class, then the class must implement the equals
(and preferably hashCode
) method. If the custom class has not overridden these methods, the indexOf
and lastIndexOf
methods will not be able to find the element in the Vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import java.util.Vector; class Student{ private int id; public Student(int id){ this.id = id; } public String toString(){ return "Student[" + this.id + "]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (id != other.id) return false; return true; } } public class VectorFindElementIndexExample { public static void main(String[] args) { Vector<Student> vStudents = new Vector<Student>(); vStudents.add(new Student(1001)); vStudents.add(new Student(1002)); vStudents.add(new Student(1003)); vStudents.add(new Student(1002)); System.out.println( vStudents.indexOf(new Student(1002)) ); System.out.println( vStudents.lastIndexOf(new Student(1002)) ); } } |
Output
1 2 |
1 3 |
This example is a part of the Vector in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation