1 -1
Often a Vector
will have several of the same item.
The indexOf()
method finds only the first.
Use the following method:
int indexOf(Object elem, int index) // Search for the first occurrence of //elem
starting atindex
The method returns the index of the first occurrence of elem
at or past the beginning index, or -1 if elem
is not found.
The method is overloaded—it has the same name as the previous method but one additional parameter.
Examine the following program. What will it print?
import java.util.* ; class VectorEg { public static void main ( String[] args) { Vector names = new Vector( 10 ); names.addElement( "Amy" ); names.addElement( "Bob" ); names.addElement( "Chris" ); names.addElement( "Deb" ); names.addElement( "Chris" ); names.addElement( "Joe" ); System.out.println( names.indexOf( "Bob", 0 ) ); System.out.println( names.indexOf( "Chris", 0 ) ); System.out.println( names.indexOf ( "Chris", names.indexOf("Chris")+1 ) ); System.out.println( names.indexOf( "Elaine", 2 ) ); } }