The Vector and Stack Classes

26 374 1
Tài liệu đã được kiểm tra trùng lặp
The Vector and Stack Classes

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 3: The Vector and Stack Classes Overview Arrays are good when you know the size of your collection and when all the elements in a collection are of the same type However, what are you to if you need a dynamically growing structure but you don't necessarily know the final size in advance? This is where the Vector class comes in handy In addition to the Vector class, Java provides a Stack class for the familiar last−in, first−out data structure Figure 3−1 shows the current hierarchy for these two classes With the Java platform, version 1.2 release, this structure changed considerably with the introduction of the Collections Framework Figure 3−2 shows the original, more simplified look of the class hierarchy from both Java 1.0 and Java 1.1 The 1.3 release remains the same as the 1.2 release shown in Figure 3−1 Figure 3−1: The Vector and Stack class hierarchy Figure 3−2: The Java 1.0/1.1 Vector and Stack class hierarchy Vector Basics You can think of Java vectors as dynamically sized arrays with synchronized access They prove to be very useful if you don't know the size of the array in advance, or you just need one that can change sizes over the lifetime of a program Any object can be stored within a vector: these items are called elements The one exception is that primitive data elements may not be stored in vectors, but since they aren't objects, this isn't really an exception The Vector class provides multiple constructors and many different methods to create, access, and modify the 21 Chapter 3: The Vector and Stack Classes data structure These are listed in Table 3−1 Table 3−1: Summary of the Vector Class VARIABLE/METHOD NAME Vector() capacityIncrement elementCount elementData modCount VERSION DESCRIPTION add() addAll() addElement() capacity() clear() clone() contains() 1.2 1.2 1.0 1.0 1.2 1.0 1.0 containsAll() copyInto() elementAt() elements() 1.2 1.0 1.0 1.0 ensureCapacity() equals() firstElement() get() hashCode() indexOf() insertElementAt() isEmpty() iterator() 1.0 1.2 1.0 1.2 1.2 1.0 1.0 1.0 1.2 lastElement() lastIndexOf() listIterator() 1.0 1.0 1.2 remove() removeAll() removeAllElements() removeElement() 1.2 1.2 1.0 1.0 1.0 / 1.2 1.0 1.0 1.0 1.2 Constructs an empty vector of the appropriate initial size Size increment for increasing vector capacity Number of elements within a vector Internal buffer for vector elements From AbstractList: used by iterator to check for concurrent modifications Adds an element to a vector Adds a collection of elements to a vector Adds an element to the end of a vector Returns the capacity of an internal buffer for a vector Clears all elements from a vector Creates a clone of a vector Checks if the vector contains an element Checks if the vector contains a collection of elements Copies elements of the vector into an array Returns an element at a specific position Returns an object from the vector that allows all of the vector's keys to be visited Ensures the capacity of an internal buffer is at least a certain size Checks for equality with another object Returns the first element within a vector Returns an element at a specific position Returns the computed hash code for a vector Searches for an element within a vector Inserts an element into the vector Checks if the vector is empty Returns an object from the vector that allows all of the vector's elements to be visited Returns the last element within a vector Searches from the end of a vector for an element Returns an object from the vector that allows all of the vector's elements to be visited sequentially Clears a specific element from the vector Clears a collection of elements from the vector Clears all elements from the vector Clears a specific element from the vector 22 Creating Vectors removeElementAt() 1.0 Clears an element at specific position from the vector removeRange() 1.2 Clears a range of elements from the vector retainAll() 1.2 Removes all elements from the vector not in another collection set() 1.2 Changes an element at a specific position within the vector setElementAt() 1.0 Changes an element at a specific position within the vector setSize() 1.0 Changes the size of an internal vector buffer size() 1.0 Returns the number of elements in a vector subList() 1.2 Returns a portion of the vector toArray() 1.2 Returns the elements of a vector as an array toString() 1.0 Converts vector contents into a string trimToSize() 1.0 Trims the capacity of internal buffer to actual size Note Tables will list inherited methods where appropriate However, they will not list those methods inherited from Object unless overridden Protected variables and methods are displayed in italics With the Java 1.0.x and Java 1.1.x versions of this class, many of the methods were flagged as final That is no longer the case You can now subclass Vector and override all methods Creating Vectors You can use one of four constructors to create a Vector For the first three constructors, an empty vector is created with an initial capacity of ten unless explicitly specified When that space becomes too small, the vector will double in size unless a different capacity increment is specified public Vector() public Vector(int initialCapacity) public Vector(int initialCapacity, int capacityIncrement) The reason for the different constructors is basically performance If you know the approximate size beforehand, try to size the vector to that size to start Otherwise, each time the vector size exceeds its capacity, a new internal array is created, which copies all the original elements to the larger new array Creating a new array and copying the elements takes time, thus increasing the time it takes to add elements to the array See the later section "Sizing Vectors" for more information on sizing and capacity Note An IllegalArgumentException will be thrown if the initial capacity sent to the constructor is negative The final constructor copies the object references in a different collection to initialize the vector: public Vector(Collection c) This effectively makes a shallow copy of the original collection The new vector is sized to be 10% larger than the number of elements in the original collection Basically, you can convert the elements of any collection (that implements Collection) into a Vector One specialty collection will be mentioned here, though If you'd like to create a vector from an array, there is a helper method available: The asList() method of the Arrays class will create an object you can pass through to the Vector constructor, as shown here: Vector v = new Vector(Arrays.asList(array)); You'll learn more about the Collection interface in Chapter at the beginning of Part Two of this book 23 Adding Elements Adding Elements Once you've created the vector, the next step is to put elements in it There are six different ways to this Adding at the End The first set involves the single−argument add() and addElement() methods as you see here: public boolean add(Object element) public void addElement(Object element) These methods are essentially the same—both add the element to the end of the vector The difference is that add() will always return true, while addElement() has no return value To demonstrate how you might use these methods, the following will fill up an array with all the elements passed from the command line: import java.util.Vector; public class InsertVector { public static void main (String args[]) { Vector v = new Vector(); for (int i=0, n=args.length; i

Ngày đăng: 05/10/2013, 12:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan