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.]

19.5.3 Vector (Continued)

The application’s constructor creates a Vector (line 13) of type String with an initial capacity of 10 elements and capacity increment of zero (the defaults for a Vector). Note that Vector is a generic class, which takes one argument that specifies the type of the elements stored in the Vector. A capacity increment of zero indicates that this Vector will double in size each time it needs to grow to accommodate more elements. Class Vector provides three other constructors. The constructor that takes one integer argument creates an empty Vector with the initial capacity specified by that argument. The constructor that takes two arguments creates a Vector with the initial capacity specified by the first argument and the capacity increment specified by the second argument. Each time the Vector needs to grow, it will add space for the specified number of elements in the capacity increment. The constructor that takes a Collection creates a copy of a collection’s elements and stores them in the Vector. Line 18 calls Vector method add to add objects (Strings in this program) to the end of the Vector. If necessary, the Vector increases its capacity to accommodate the new element. Class Vector also provides a method add that takes two arguments. This method takes an object and an integer and inserts the object at the specified index in the Vector. Method set will replace the element at a specified position in the Vector with a specified element. Method insertElementAt provides the same functionality as the method add that takes two arguments, except that the order of the parameters is reversed. Line 25 calls Vector method firstElement to return a reference to the first element in the Vector. Line 26 calls Vector method lastElement to return a reference to the last element in the Vector. Each of these methods throws a NoSuchElementException if there are no elements in the Vector when the method is called. Line 35 calls Vector method contains to determine whether the Vector contains "red". The method returns true if its argument is in the Vector—otherwise, the method returns false. Method contains uses Object method equals to determine whether the search key is equal to one of the Vector’s elements. Many classes override method equals to perform the comparisons in a manner specific to those classes. For example, class String declares equals to compare the individual characters in the two Strings being compared. If method equals is not overridden, the original version of method equals inherited from class Object is used.

Common Programming Error
Common Programming Error 19.4
Without overriding method equals, the program performs comparisons using operator == to determine whether two references refer to the same object in memory.
Line 37 calls Vector method indexOf to determine the index of the first location in the Vector that contains the argument. The method returns −1 if the argument is not found in the Vector. An overloaded version of this method takes a second argument specifying the index in the Vector at which the search should begin.

Performance Tip
Performance Tip 19.6
Vector methods contains and indexOf perform linear searches of a Vector’s contents. These searches are inefficient for large Vectors. If a program frequently searches for elements in a collection, consider using one of the Java Collection API’s Map implementations (Section 19.10), which provide high-speed searching capabilities.
Line 41 calls Vector method remove to remove the first occurrence of its argument from the Vector. The method returns true if it finds the element in the Vector; otherwise, the method returns false. If the element is removed, all elements after that element in the Vector shift one position toward the beginning of the Vector to fill in the position of the removed element. Class Vector also provides method removeAllElements to remove every element from a Vector and method removeElementAt to remove the element at a specified index. Lines 53–54 use Vector methods size and capacity to determine the number of elements currently in the Vector and the number of elements that can be stored in the Vector without allocating more memory, respectively. Line 59 calls Vector method isEmpty to determine whether the Vector is empty. The method returns true if there are no elements in the Vector; otherwise, the method returns false. Lines 66–67 use the enhanced for statement to print out all elements in the vector. Among the methods introduced in Fig. 19.6, firstElement, lastElement and capacity can be used only with Vector. Other methods (e.g., add, contains, indexOf, remove, size and isEmpty) are declared by List, which means that they can be used by any class that implements List, such as Vector.

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