Vector class indexOf(Object o, int index)method example
Returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found.
Vector class indexOf(Object o, int index)method example. This example shows you how to use indexOf(Object o, int index)method.This method Returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found.
Here is the code:-
/*
* @Program that Returns the index of the first occurrence of the specified
element in this vector,
* IndexOf1.java
* Author:-RoseIndia Team
* Date:-26-May-2008
*/
import java.util.*;
public class IndexOf1 {
public static void main(String[] args) {
//Creating vector
Vector v = new Vector();
//Adding element to the vector
v.add("Rose");
v.add("India");
v.add("Rohini");
v.add("Girish");
v.add("Rose");
//Creating Enumeration Interface
Enumeration e = v.elements();
System.out.print("Vector created is: ");
while (e.hasMoreElements()) {
System.out.print(e.nextElement() + "\t");
}
System.out.println();
//Returns the index of the first occurrence of the specified element in this
//vector, searching forwards from index, or returns -1 if the element is not
//found.
int i = v.indexOf("Rose", 1);
System.out.println("Index of Rose in the vector after Specified index is : " + i);
}
} |
Output of the program:-
Vector created is: Rose India Rohini Girish Rose
Index of Rose in the vector after Specified index is : 4 |
|