A good answer might be:

0: Amy
1: Bob
2: Deb

Inserting Elements

Sometimes you wish to insert an element into a Vector at a particular index. When an element is inserted at index the element previously at index is moved up to index+1, and so on until the element previously at size()-1 is moved up to size(). The size of the Vector has now increased by one, and the capacity will increase if more room is needed.

insertElementAt(Object element, int index)  // Inserts the element at index. 
                                            // Each element with an index 
                                            // equal or greater than index is 
                                            // shifted upward by one more than
                                            // its previous value. 

Inserting is different from setting an element. When setElementAt(objectReference, index ) is used, the object reference previously at index is replaced by the new objectReference. No other elements are effected, and the size does not change.

QUESTION 12:

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.insertElementAt( "Elaine", 2);

    for ( int j=0; j < names.size(); j++ )
      System.out.println( j + ": " + names.elementAt(j) ); 

  }
}