Java Reference
In-Depth Information
20.24
How do you modify the code in the MutilpleBallApp program to remove the first
ball in the list when the button is clicked?
20.25
How do you modify the code in the MutilpleBallApp program so that each ball
will get a random radius between 10 and 20?
20.8 Vector and Stack Classes
Vector is a subclass of AbstractList , and Stack is a subclass of Vector in the
Java API.
Key
Point
The Java Collections Framework was introduced in Java 2. Several data structures were sup-
ported earlier, among them the Vector and Stack classes. These classes were redesigned
to fit into the Java Collections Framework, but all their old-style methods are retained for
compatibility.
Vector is the same as ArrayList , except that it contains synchronized methods for
accessing and modifying the vector. Synchronized methods can prevent data corruption
when a vector is accessed and modified by two or more threads concurrently. We will dis-
cuss synchronization in ChapterĀ 30, Multithreading and Parallel Programming. For the many
applications that do not require synchronization, using ArrayList is more efficient than
using Vector .
The Vector class extends the AbstractList class. It also has the methods contained in
the original Vector class defined prior to Java 2, as shown in FigureĀ 20.10.
java.util.AbstractList<E>
java.util.Vector < E >
+Vector()
+Vector(c: Collection<? extends E>)
+Vector(initialCapacity: int)
+Vector(initCapacity: int, capacityIncr: int)
+addElement(o: E): void
+capacity(): int
+copyInto(anArray: Object[]): void
+elementAt(index: int): E
+elements(): Enumeration<E>
+ensureCapacity(): void
+firstElement(): E
+insertElementAt(o: E, index: int): void
+lastElement(): E
+removeAllElements(): void
+removeElement(o: Object): boolean
+removeElementAt(index: int): void
+setElementAt(o: E, index: int): void
+setSize(newSize: int): void
+trimToSize(): void
Creates a default empty vector with initial capacity 10.
Creates a vector from an existing collection.
Creates a vector with the specified initial capacity.
Creates a vector with the specified initial capacity and increment.
Appends the element to the end of this vector.
Returns the current capacity of this vector.
Copies the elements in this vector to the array.
Returns the object at the specified index.
Returns an enumeration of this vector.
Increases the capacity of this vector.
Returns the first element in this vector.
Inserts o into this vector at the specified index.
Returns the last element in this vector.
Removes all the elements in this vector.
Removes the first matching element in this vector.
Removes the element at the specified index.
Sets a new element at the specified index.
Sets a new size in this vector.
Trims the capacity of this vector to its size.
F IGURE 20.10
Starting in Java 2, the Vector class extends AbstractList and also retains all the methods in the original
Vector class.
 
 
 
Search WWH ::




Custom Search