© 1998-2002 McGraw-Hill

structure
Class Vector

java.lang.Object
  |
  +--structure.AbstractStructure
        |
        +--structure.AbstractList
              |
              +--structure.Vector
All Implemented Interfaces:
Cloneable, List, Structure

public class Vector
extends AbstractList
implements Cloneable

An implementation of extensible arrays, similar to that of java.util.Vector. This vector class implements a basic extensible array. It does not implement any of the additional features of the Sun class, including list-like operations. Those operations are available in other implementors of List in this package.

Example usage: To put a program's parameters into a Vector, we would use the following:

 public static void main(String[] arguments)
 {
    Vector argVec = new Vector();
    for (int i = 0; i < arguments.length; i++)
    {
       argVec.add(arguments[i]);
    }
    System.out.println(argVec);
 }
 

Since:
JavaStructures 1.0

Field Summary
protected  int capacityIncrement
          The size of size increment, should the vector become full.
protected static int defaultCapacity
          The default size of the vector; may be overridden in the Vector(int) constructor.
protected  int elementCount
          The actual number of elements logically stored within the vector.
protected  Object[] elementData
          The data associated with the vector.
protected  Object initialValue
          The initial value of any new elements that are appended to the vector.
 
Constructor Summary
Vector()
          Construct an empty vector.
Vector(Collection c)
           
Vector(int initialCapacity)
          Construct an empty vector capable of storing initialCapacity values before the vector must be extended.
Vector(int initialCapacity, int capacityIncr)
          Construct a vector with initial capacity, and growth characteristic.
Vector(int initialCapacity, int capacityIncr, Object initValue)
          Construct a vector with initial size, growth rate and default value.
Vector(Vector that)
           
 
Method Summary
 void add(int index, Object obj)
          Insert an element at a particular location.
 void add(Object obj)
          Add an element to the high end of the array, possibly expanding vector.
 void addElement(Object o)
          Add an element to the high end of the array, possibly expanding vector.
 int capacity()
          Determine the capacity of the vector.
 void clear()
          Remove all the values of the vector.
 Object clone()
          Construct a shallow copy of the vector.
 boolean contains(Object elem)
          Determine if a value appears in a vector.
 void copyInto(Object[] dest)
          Copy the contents of the vector into an array.
 Object elementAt(int index)
          Fetch the element at a particular index.
 void ensureCapacity(int minCapacity)
          Ensure that the vector is capable of holding at least minCapacity values without expansion.
 Object firstElement()
          Get access to the first element of the vector.
 Object get(int index)
          Fetch the element at a particular index.
 int indexOf(Object elem)
          Assuming the data is not in order, find the index of a value, or return -1 if not found.
 int indexOf(Object elem, int index)
          Assuming the data is not in order, find the index of a value or return -1 if the value is not found.
 void insertElementAt(Object obj, int index)
          Insert an element at a particular location.
 boolean isEmpty()
          Determine if the Vector contains no values.
 Iterator iterator()
          Construct a iterator over the elements of the vector.
 Object lastElement()
          Fetch a reference to the last value in the vector.
 int lastIndexOf(Object obj)
          Search for the last occurrence of a value within the vector.
 int lastIndexOf(Object obj, int index)
          Find the index of the last occurrence of the value in the vector before the indexth position.
 Object remove(int where)
          Remove an element at a particular location.
 Object remove(Object element)
          Remove an element, by value, from vector.
 void removeAllElements()
          Remove all the elements of the vector.
 void removeElementAt(int where)
          Remove an element at a particular location.
 Object set(int index, Object obj)
          Change the value stored at location index.
 void setElementAt(Object obj, int index)
          Change the value stored at location index.
 void setSize(int newSize)
          Explicitly set the size of the array.
 int size()
          Determine the number of elements in the vector.
 String toString()
          Determine a string representation for the vector.
 void trimToSize()
          Trim the vector to exactly the correct size.
 
Methods inherited from class structure.AbstractList
addFirst, addLast, get, getFirst, getLast, remove, removeFirst, removeLast
 
Methods inherited from class structure.AbstractStructure
elements, hashCode, values
 
Methods inherited from class java.lang.Object
, equals, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 
Methods inherited from interface structure.Structure
elements, values
 

Field Detail

elementData

protected Object[] elementData
The data associated with the vector. The size of the array is always at least as large as the vector. The array is only extended when necessary.

elementCount

protected int elementCount
The actual number of elements logically stored within the vector. May be smaller than the actual length of the array.

capacityIncrement

protected int capacityIncrement
The size of size increment, should the vector become full. 0 indicates the vector should be doubled when capacity of the array is reached.

initialValue

protected Object initialValue
The initial value of any new elements that are appended to the vector. Normally null. Be aware that references used in this way will result in multiple references to a single object.

defaultCapacity

protected static final int defaultCapacity
The default size of the vector; may be overridden in the Vector(int) constructor.
Constructor Detail

Vector

public Vector()
Construct an empty vector.

Vector

public Vector(int initialCapacity)
Construct an empty vector capable of storing initialCapacity values before the vector must be extended.
Parameters:
initialCapacity - The size of vector before reallocation is necessary

Vector

public Vector(int initialCapacity,
              int capacityIncr)
Construct a vector with initial capacity, and growth characteristic.
Parameters:
initialCapacity - The initial number of slots in vector.
capacityIncr - The size of growth of vector.
See Also:
capacityIncrement

Vector

public Vector(int initialCapacity,
              int capacityIncr,
              Object initValue)
Construct a vector with initial size, growth rate and default value.
Parameters:
initialCapacity - The initial number of slots in vector.
capacityIncr - The size of the increment when vector grows.
initValue - The initial value stored in vector elements.

Vector

public Vector(Vector that)

Vector

public Vector(Collection c)
Method Detail

ensureCapacity

public void ensureCapacity(int minCapacity)
Ensure that the vector is capable of holding at least minCapacity values without expansion.
Parameters:
minCapacity - The minimum size of array before expansion.
Postcondition:
the capacity of this vector is at least minCapacity

add

public void add(Object obj)
Add an element to the high end of the array, possibly expanding vector.
Overrides:
add in class AbstractList
Parameters:
obj - The object to be added to the end of the vector.
Postcondition:
adds new element to end of possibly extended vector

addElement

public void addElement(Object o)
Add an element to the high end of the array, possibly expanding vector.
Parameters:
obj - The object to be added to the end of the vector.
Postcondition:
adds new element to end of possibly extended vector

remove

public Object remove(Object element)
Remove an element, by value, from vector.
Parameters:
element - the element to be removed.
Postcondition:
element equal to parameter is removed and returned
Returns:
the element actually removed, or if none, null.

capacity

public int capacity()
Determine the capacity of the vector. The capacity is always at least as large as its size.
Postcondition:
returns allocated size of the vector
Returns:
The size of the array underlying the vector.

clone

public Object clone()
Construct a shallow copy of the vector. The vector store is copied, but the individual elements are shared objects.
Overrides:
clone in class Object
Postcondition:
returns a copy of the vector, using same objects
Returns:
A copy of the original vector.

contains

public boolean contains(Object elem)
Determine if a value appears in a vector.
Overrides:
contains in class AbstractList
Parameters:
elem - The value sought.
Postcondition:
returns true iff Vector contains the value (could be faster, if orderedVector is used)
Returns:
True iff the value appears in the vector.

copyInto

public void copyInto(Object[] dest)
Copy the contents of the vector into an array. The array must be large enough to accept all the values in the vector.
Parameters:
dest - An array of size at least size().
Precondition:
dest has at least size() elements
Postcondition:
a copy of the vector is stored in the dest array

elementAt

public Object elementAt(int index)
Fetch the element at a particular index. The index of the first element is zero.
Parameters:
index - The index of the value sought.
Precondition:
0 <= index && index < size()
Postcondition:
returns the element stored in location index
Returns:
A reference to the value found in the vector.

get

public Object get(int index)
Fetch the element at a particular index. The index of the first element is zero.
Parameters:
index - The index of the value sought.
Precondition:
0 <= index && index < size()
Postcondition:
returns the element stored in location index
Returns:
A reference to the value found in the vector.

iterator

public Iterator iterator()
Construct a iterator over the elements of the vector. The iterator considers elements with increasing index.
Postcondition:
returns an iterator allowing one to view elements of vector
Returns:
an iterator to traverse the vector.

firstElement

public Object firstElement()
Get access to the first element of the vector.
Precondition:
vector contains an element
Postcondition:
returns first value in vector
Returns:
Access to the first element of the vector.

indexOf

public int indexOf(Object elem)
Assuming the data is not in order, find the index of a value, or return -1 if not found.
Parameters:
elem - The value sought in vector.
Postcondition:
returns index of element equal to object, or -1; starts at 0
Returns:
The index of the first occurrence of the value.

indexOf

public int indexOf(Object elem,
                   int index)
Assuming the data is not in order, find the index of a value or return -1 if the value is not found. Search starts at index.
Parameters:
elem - The value sought.
index - The first location considered.
Postcondition:
returns index of element equal to object, or -1; starts at index
Returns:
The index of the first location, or -1 if not found.

insertElementAt

public void insertElementAt(Object obj,
                            int index)
Insert an element at a particular location. Vector is grown as needed
Parameters:
obj - The value to be inserted.
index - The location of the new value.
Precondition:
0 <= index <= size()
Postcondition:
inserts new value in vector with desired index, moving elements from index to size()-1 to right

add

public void add(int index,
                Object obj)
Insert an element at a particular location. Vector is grown as needed
Parameters:
obj - the value to be inserted.
index - the location of the new value.
Precondition:
0 <= index <= size()
Postcondition:
inserts new value in vector with desired index, moving elements from index to size()-1 to right

isEmpty

public boolean isEmpty()
Determine if the Vector contains no values.
Overrides:
isEmpty in class AbstractList
Postcondition:
returns true iff there are no elements in the vector
Returns:
True iff the vector is empty.

lastElement

public Object lastElement()
Fetch a reference to the last value in the vector.
Precondition:
vector is not empty
Postcondition:
returns last element of the vector
Returns:
A reference to the last value.

lastIndexOf

public int lastIndexOf(Object obj)
Search for the last occurrence of a value within the vector. If none is found, return -1.
Parameters:
obj - The value sought.
Postcondition:
returns index of last occurrence of object in the vector, or -1
Returns:
The index of the last occurrence in the vector.

lastIndexOf

public int lastIndexOf(Object obj,
                       int index)
Find the index of the last occurrence of the value in the vector before the indexth position.
Parameters:
obj - The value sought.
index - The last acceptable index.
Precondition:
index >= 0
Postcondition:
returns the index of last occurrence of object at or before index
Returns:
The index of the last occurrence of the value, or -1 if none.

clear

public void clear()
Remove all the values of the vector.
Postcondition:
vector is empty

removeAllElements

public void removeAllElements()
Remove all the elements of the vector. Kept for compatibility with java.util.Vector.
Postcondition:
vector is empty
See Also:
clear()

removeElementAt

public void removeElementAt(int where)
Remove an element at a particular location.
Parameters:
where - The location of the element to be removed.
Precondition:
0 <= where && where < size()
Postcondition:
indicated element is removed, size decreases by 1

remove

public Object remove(int where)
Remove an element at a particular location.
Parameters:
where - The location of the element to be removed.
Precondition:
0 <= where && where < size()
Postcondition:
indicated element is removed, size decreases by 1

setElementAt

public void setElementAt(Object obj,
                         int index)
Change the value stored at location index.
Parameters:
obj - The new value to be stored.
index - The index of the new value.
Precondition:
0 <= index && index < size()
Postcondition:
element value is changed to obj

set

public Object set(int index,
                  Object obj)
Change the value stored at location index.
Parameters:
obj - The new value to be stored.
index - The index of the new value.
Precondition:
0 <= index && index < size()
Postcondition:
element value is changed to obj; old value is returned

setSize

public void setSize(int newSize)
Explicitly set the size of the array. Any new elements are initialized to the default value.
Parameters:
newSize - The ultimate size of the vector.
Precondition:
newSize >= 0
Postcondition:
vector is resized, any new elements are initialized

size

public int size()
Determine the number of elements in the vector.
Postcondition:
returns the size of the vector
Returns:
The number of elements within the vector.

trimToSize

public void trimToSize()
Trim the vector to exactly the correct size.
Postcondition:
minimizes allocated size of vector

toString

public String toString()
Determine a string representation for the vector.
Overrides:
toString in class Object
Postcondition:
returns a string representation of vector
Returns:
A string representation for the vector.

© 1998-2002 McGraw-Hill