Vectors and Enumerators as a word document

advertisement
Vectors
In order to use the java predefined Vector class is necessary to import java.util.Vector. Since Vectors require some
other utilities, if memory is not a problem, it is better if the whole library is imported. A vector is similar to an array
of objects except that when the specified capacity is exceeded ,the Vector automatically grows by a specified
increment amount. This amount is specified in the constructor.
Arrays Vs. Vectors
The power from the array comes from the fact that the time required to access any element in the collection is
constant regardless of the size of the array. We say that the array permits random access, as the order that the
elements are accesses can be entirely arbitrary. This has a downside. The constant-time access derives from the fact
that the elements in the array are stored in a block in which the elements are located end to end.
element[0] element[1] element[2] element[3] element[4]
If we need to make an array larger we have a problem. If we need to insert elements in the middle of the collection,
we have do do a lot of work. I we need to hold a set of different kinds of objects/primitive-data-types, we can't. As
we can see, even with all its advantages, it is not appropriate to solve all kinds of problems.
Vectors are stored in memory wherever space is available, therefore the elements that are virtually next to each other
may be stored far away in memory. A vector can store as many objects as needed as long as there is memory
available. The objects stored don't need to be of the same type. The programmer doesn't need to know in advance the
approximate size of the data collection being stored. Since in everything there is a downside, vectors can't store
primitive data types, they can only store objects. In order to store primitive data types it is necessary to transform
these data into objects using the wrapper classes.
Main differences between Vectors and Arrays:

An array is not a class. It is not defined in a class library. It is built into the language itself. The vector is a
predefined Java collection class included in the util library.

There are no "array methods" to work with arrays or their elements; instead, special symbols are provided
by the language tat allow us to access array elements.

Array elements can hold primitive data such as integer values as well as references to objects while Vectors
can only contain references to objects.

The type of the elements of an array must be specified in the array's declaration. Everything stored in its
elements must be of that type. The case is different with Vectors where each element can be of a different
kind.

Arrays can't grow, once an array has been created it has a fixed number of elements. Vectors can grow
dynamically.
Vector constructors:
Vector( )
Creates an empty vector with an initial capacity of 10 and increment
amount of zero.
Vector (int initialCapacity)
Creates an empty Vector with the specified initial capacity and an
increment amount of zero
Vector (int initialCapacity, int
capacityIncrement)
Creates an empty Vector with the specified initial capacity and a
specified increment amount.
Accessor Methods
public int capacity( )
Returns the current capacity of this vector. The add operations work
efficiently without allocating new memory until this capacity is
exceeded.
public boolean contains (Object obj)
The obj.equals method is used to determine whether obj occurs
anywhere in the Vector
public Object elementAt(int index)
Returns a reference to the element at the given index of this Vector.
public Object firstElement( )
Returns a reference to the first element of this Vector (at index [0])
public int indexOf(Object obj)
Searches for the first occurence of obj in this Vector, using
obj.equals to test for equality. Returns the index of this occurrence or
-1 if it doesn't occur.
public int indexOf(Object obj)
Searches for the first occurrence of obj in this Vector, at ot after the
given index and using obj.equals to test for equality. Returns the
index of this occurrence or -1 if it doesn't occur.
public boolean isEmpty( )
Tests whether the vector is empty
public Object lastElement( )
Returns a reference to the last element of this Vector. At the highest
index.
public int lastIndexOf( Object obj)
Searches for the final occurrence of obj in this Vector, using
obj.equals to test for equality. Returns the index of this occurrence or
-1 if it doesn't occur.
Searches for the final occurrence of obj in this Vector at or before the
public int lastIndexOf( Object obj, int
given index and using obj.equals to test for equality. Returns the
index)
index of this occurrence or -1 if it doesn't occur.
public int size( )
Returns the number of elements currently in this Vector.
Modification methods
public void addElement (Object obj)
Adds the specified element to this Vector at the next
available index.
Public void ensureCapacity ( int
minimumCapacity)
Resets the current capacity of the vector to at least
minimumCapacity. The add operations work efficiently
(without allocating memory until the capacity is
exceeded).
public void insertElementAt ( Object obj, int
index)
Adds the specified element to this Vector at the given
index (which may be no bigger than the current size)
Other elements at and after this index are shifted upward
to make room for the new element.
public void removeAllElements ( )
Resets the Vector so that it is empty.
public boolean removeElement (Object obj)
Removes the first occurrence of the specified element
from this Vector. The obj.equals method is used to find
this occurrence, and the return value indicates whether
or not that element was found.
public void removeElementAt ( int index)
Removes the element of this Vector from the specified
index (which must be less than the current size). Other
elements that were after this index have been shifted
downward one spot.
public void setElementAt (Object obj, int index)
Changes the element of this vector at the given index
(which must be less than the current size). The new
value of this is obj, and the older value is no longer
present at this index.
public void setSize (int newSize)
Sets the size of this Vector. If the new size is greater
than the current size, new null items are added to the
end. If the new size is less than the current size, all
components beyond the new size are removed.
public void trimToSize( )
Sets the current capacity of this Vector equal to the
elements it contains.
Enumerations
To enumerate a collection is to examine all the elements in turn, one at a time without repetition. The process of
going through a collection of objects is known as a traversal of the collection, and we thus speak of traversing the
collection and visiting (processing) each element. A traversal or iteration is a repetitive process so we shouldn't be
surprised that a loop is involved. Given the methods that we have already mentioned/learned you could think of
performing vector traversal as follows:
for (int i = 0; i< aVec.size( ); i++){
Object v = aVec.elementAt(i);
.... //do something with v
}
However, a Vector can be traversed in a different way. This is the way that we are interested in learning in this
course. We will be using a new class called: Enumeration. We might expect Vector to provide these methods but it
is not the only collection class that needs an enumerator so the Enumeration class is shared among several JAVA
collection classes. An enumeration is an object whose job is to move through a sequence of objects and select each
in that sequence without the programmer having to take care of all the details. Some iterators can only move in one
direction. This is the case of the JAVA Enumeration.
To create and Enumeration object and associate it with a Vector we do the following:
The vector can be populated as follows:
Vector v = new Vector( );
for (int i=0; i<20; i++) {
Object o = new Object( );
v.addElement(o);
}
After filling up the Vector with elements it can be traversed using an Enumeration object. An Enumeration models
traversal behavior. Each collection class (The Vector is a collection class) provides a method that creates and
returns a reference to an object of class Enumeration. It is the Enumeration object that provides the methods to get
the next element and test for more elements.
boolean hasMoreElements( ) returns true if the vector has more elements after the one that it is currently visiting.
Object nextElement( ) returns the element that is in the next position and advances into that position.
Enumeration e = v.elements( );
while( e.hasMoreElements( ) ){
obj = e.nextElement();
}
These notes are a compilation made by Blanca Polo (Jan 21st, 2002) from the following books:

Classic Data Structures in JAVA by Timothy Budd

Data structures and other objects using JAVA by Michael Main

Introduction to programming using JAVA by Arnow and Weiss

JAVA software solutions by Lewis and Loftus.

Thinking in JAVA by Bruce Eckel
Download