capacity: 20 size: 0 capacity: 20 size: 3
Vector
Elements
You just saw that the addElement()
method adds
to the end of a Vector
.
Sometimes you want to set the data at a particular index.
setElementAt( objectReference, index )The
index
should be within 0 to size-1.
The data previously at index
is replaced with objectReference.
To access the object at a particular index use:
elementAt( index )The
index
should be 0 to size-1.
Here is the example program:
import java.util.* ; class VectorEg { public static void main ( String[] args) { Vector names = new Vector( 20, 5 ); names.addElement("Amy"); names.addElement("Bob"); names.addElement("Cindy"); System.out.println("slot 0: " + names.elementAt(0) ); System.out.println("slot 1: " + names.elementAt(1) + "\n"); names.setElementAt( "Zoe", 0 ); System.out.println("slot 0: " + names.elementAt(0) ); System.out.println("slot 1: " + names.elementAt(1) ); } } |