Source of VectorTest.java


  1: // Fig. 19.6: VectorTest.java
  2: // Using the Vector class.
  3: import java.util.Vector;
  4: import java.util.NoSuchElementException;
  5: 
  6: public class VectorTest 
  7: {
  8:    private static final String colors[] = { "red", "white", "blue" };
  9: 
 10:    public VectorTest()
 11:    {
 12:       Vector< String > vector = new Vector< String >();
 13:       printVector( vector ); // print vector
 14: 
 15:       // add elements to the vector
 16:       for ( String color : colors )
 17:          vector.add( color );   
 18: 
 19:       printVector( vector ); // print vector
 20:       
 21:       // output the first and last elements
 22:       try 
 23:       {
 24:          System.out.printf( "First element: %s\n", vector.firstElement());
 25:          System.out.printf( "Last element: %s\n", vector.lastElement() );
 26:       } // end try
 27:       // catch exception if vector is empty
 28:       catch ( NoSuchElementException exception ) 
 29:       {
 30:          exception.printStackTrace();
 31:       } // end catch
 32:       
 33:       // does vector contain "red"?
 34:       if ( vector.contains( "red" ) )
 35:          System.out.printf( "\n\"red\" found at index %d\n\n", 
 36:             vector.indexOf( "red" ) );
 37:       else
 38:          System.out.println( "\n\"red\" not found\n" );
 39:       
 40:       vector.remove( "red" ); // remove the string "red"
 41:       System.out.println( "\"red\" has been removed" );
 42:       printVector( vector ); // print vector
 43:       
 44:       // does vector contain "red" after remove operation?
 45:       if ( vector.contains( "red" ) )
 46:          System.out.printf( 
 47:             "\"red\" found at index %d\n", vector.indexOf( "red" ) );
 48:       else
 49:          System.out.println( "\"red\" not found" );
 50:       
 51:       // print the size and capacity of vector
 52:       System.out.printf( "\nSize: %d\nCapacity: %d\n", vector.size(), 
 53:          vector.capacity() );
 54:    } // end VectorTest constructor
 55:    
 56:    private void printVector( Vector< String > vectorToOutput )
 57:    {
 58:       if ( vectorToOutput.isEmpty() ) 
 59:          System.out.print( "vector is empty" ); // vectorToOutput is empty
 60:       else  // iterate through the elements
 61:       { 
 62:          System.out.print( "vector contains: " );      
 63: 
 64:          // output elements
 65:          for ( String element : vectorToOutput )
 66:             System.out.printf( "%s ", element );
 67:       } // end else
 68:       
 69:       System.out.println( "\n" ); 
 70:    } // end method printVector
 71: 
 72:    public static void main( String args[] )
 73:    {
 74:       new VectorTest(); // create object and call its constructor
 75:    } // end main
 76: } // end class VectorTest
 77: 
 78: /**************************************************************************
 79:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 80:  * Pearson Education, Inc. All Rights Reserved.                           *
 81:  *                                                                        *
 82:  * DISCLAIMER: The authors and publisher of this book have used their     *
 83:  * best efforts in preparing the book. These efforts include the          *
 84:  * development, research, and testing of the theories and programs        *
 85:  * to determine their effectiveness. The authors and publisher make       *
 86:  * no warranty of any kind, expressed or implied, with regard to these    *
 87:  * programs or to the documentation contained in these books. The authors *
 88:  * and publisher shall not be liable in any event for incidental or       *
 89:  * consequential damages in connection with, or arising out of, the       *
 90:  * furnishing, performance, or use of these programs.                     *
 91:  *************************************************************************/