public final intcapacity()
No analogue. Returns the current capacity of the vector.
In addition to these public methods, protected fields are available to classes that subclass the Vector class.
Be careful what you do (if anything) with these fields because, for example, methods in Vector rely on
elementCount being less than or equal to the length of the elementData array.
protected Object[]elementData
The buffer where elements are stored.
protected intelementCount
The number of elements currently used in the buffer.
protected intcapacityIncrement
The number of elements to add to the capacity when elementData runs
out of space. If zero, the size of the buffer is doubled every time it needs to
grow.
21.15.3. Stack
The Stack
push to push an object onto the stack and use pop to remove the top element from the stack. The peek
method returns the top item on the stack without removing it. The empty method returns TRue if the stack is
empty. Trying to pop or peek in an empty Stack object will throw an EmptyStackException.
You can use search to find an object's distance from the top of the stack, with 1 being the top of the stack. If
the object isn't found, 1 is returned. The search method uses Object.equals to test whether an object in
the stack is the same as the one it is searching for.
These methods are trivial uses of the Vector methods, so using an ArrayList to implement a Stack
would be simple: using add to implement push, and so on. There is no analogue to Stack in the
collections.
Exercise 21.7: Implement a stack using ArrayList. Should your stack class be a subclass of ArrayList
or use an ArrayList internally, providing different stack-specific methods?
21.15.4. Dictionary
The Dictionary<K,V> abstract class is essentially an interface. It is analogous to the Map interface but
uses the terms key and element instead of key and value. With two exceptions, each method in Dictionary
has the same name as its analogous method in Map: get, put, remove, size, and isEmpty. The two
exceptions are keys and elements. In Map, you get a Set of keys or values that you can iterate over or
otherwise manipulate. In Dictionary, you can only get an Enumeration (iterator) for the keys and
elements, using the keys and elements methods, respectively. The legacy collections did not contain a
Set type, and so Dictionary could not be expressed in those terms.