Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

In our September 10, 2005 newsletter, we provided a three-part tutorial that introduced Java generics and showed how to create your own generic methods. In this four-part tutorial,  we present Java 5.0's List collections, which are now implemented as generic classes. Part 1 overviews the classes of the java.util package that implement the List interface-ArrayList, LinkedList and Vector. Parts 2, 3 and 4 present code examples that demonstrate each of these classes. We use various List capabilities and show how iterators can be used to traverse collections to access (and possibly modify) their elements. We also demonstrate Java 5.0's enhanced for statement, which uses a collection's iterator to traverse the collection. This tutorial is intended for students who are already familiar with Java and for Java developers.

[Note: This series of four tutorials (Part 1, Part 2, Part 3, Part 4) is an excerpt (Section 19.5) of Chapter 19, Collections, from our textbook Java How to Program, 6/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.911-922. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

Vector (Continued)

Figure 19.6 demonstrates class Vector and several of its methods. For complete information on class Vector, please visit java.sun.com/j2se/5.0/docs/api/java/util/Vector.html.

   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 Vector 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
 
  vector is empty

  vector contains: red white blue

  First element: red
  Last element: blue

  "red" found at index 0

  "red" has been removed
  vector contains: white blue

  "red" not found

  Size: 2
  Capacity: 10
 Fig. 19.6  Vector class of package java.util.

 

Page 1 | 2 | 3

Other Tutorials in this series:

Part 1: Introduction to Lists
Part 2: ArrayList and Iterator
Part 3: LinkedList
Part 4: Vector (Your are here)

Tutorial Index