names.insertElementAt( "Elaine", 2);
0: Amy 1: Bob 2: Elaine 3: Chris 4: Deb
As requested, "Elaine" is inserted at position 2 and all elements previously at position 2 and higher are moved up one to make room.
firstElement()
and lastElement()
The firstElement()
and lastElement()
methods
are sometimes useful.
They do what you imagine:
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" ); System.out.println( names.firstElement() ); System.out.println( names.lastElement() ); } }
The program prints out:
Amy Deb