structure
Class Vector<ELTTYPE>

java.lang.Object
  extended by structure.AbstractStructure<ELTTYPE>
      extended by structure.AbstractList<ELTTYPE>
          extended by structure.Vector<ELTTYPE>
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<ELTTYPE>, List<ELTTYPE>, Structure<ELTTYPE>

public class Vector<ELTTYPE>
extends AbstractList<ELTTYPE>
implements java.lang.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<String> argVec = new Vector<String>();
    for (int i = 0; i < arguments.length; i++)
    {
       argVec.add(arguments[i]);
    }
    System.out.println(argVec);
 }
 

Since:
JavaStructures 1.0

Constructor Summary
Vector()
          Construct an empty vector.
Vector(java.util.Collection<ELTTYPE> 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, ELTTYPE initValue)
          Construct a vector with initial size, growth rate and default value.
Vector(Vector<ELTTYPE> that)
           
 
Method Summary
 void add(ELTTYPE obj)
          Add an element to the high end of the array, possibly expanding vector.
 void add(int index, ELTTYPE obj)
          Insert an element at a particular location.
 void addElement(ELTTYPE 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.
 java.lang.Object clone()
          Construct a shallow copy of the vector.
 boolean contains(ELTTYPE elem)
          Determine if a value appears in a vector.
 void copyInto(ELTTYPE[] dest)
          Copy the contents of the vector into an array.
 ELTTYPE 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.
 ELTTYPE firstElement()
          Get access to the first element of the vector.
 ELTTYPE get(int index)
          Fetch the element at a particular index.
 int indexOf(ELTTYPE elem)
          Assuming the data is not in order, find the index of a value, or return -1 if not found.
 int indexOf(ELTTYPE 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(ELTTYPE obj, int index)
          Insert an element at a particular location.
 boolean isEmpty()
          Determine if the Vector contains no values.
 java.util.Iterator<ELTTYPE> iterator()
          Construct a iterator over the elements of the vector.
 ELTTYPE lastElement()
          Fetch a reference to the last value in the vector.
 int lastIndexOf(ELTTYPE obj)
          Search for the last occurrence of a value within the vector.
 int lastIndexOf(ELTTYPE obj, int index)
          Find the index of the last occurrence of the value in the vector before the indexth position.
 ELTTYPE remove(ELTTYPE element)
          Remove an element, by value, from vector.
 ELTTYPE remove(int where)
          Remove an element at a particular location.
 void removeAllElements()
          Remove all the elements of the vector.
 void removeElementAt(int where)
          Remove an element at a particular location.
 ELTTYPE set(int index, ELTTYPE obj)
          Change the value stored at location index.
 void setElementAt(ELTTYPE 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.
 java.lang.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, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure.Structure
elements, values
 

Constructor Detail

Vector

public Vector()
Construct an empty vector.

Post:
constructs a vector with capacity for 10 elements

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
Pre:
initialCapacity >= 0
Post:
constructs an empty vector with initialCapacity capacity

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
Pre:
initialCapacity >= 0, capacityIncr >= 0
Post:
constructs an empty vector with initialCapacity capacity that extends capacity by capacityIncr, or doubles if 0

Vector

public Vector(int initialCapacity,
              int capacityIncr,
              ELTTYPE 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.
Pre:
initialCapacity, capacityIncr >= 0
Post:
constructs empty vector with capacity that begins at initialCapacity and extends by capacityIncr or doubles if 0. New entries in vector are initialized to initValue.

Vector

public Vector(Vector<ELTTYPE> that)

Vector

public Vector(java.util.Collection<ELTTYPE> 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.
Post:
the capacity of this vector is at least minCapacity

add

public void add(ELTTYPE obj)
Add an element to the high end of the array, possibly expanding vector.

Specified by:
add in interface List<ELTTYPE>
Specified by:
add in interface Structure<ELTTYPE>
Overrides:
add in class AbstractList<ELTTYPE>
Parameters:
obj - The object to be added to the end of the vector.
See Also:
AbstractList.addLast(ELTTYPE)
Post:
adds new element to end of possibly extended vector

addElement

public void addElement(ELTTYPE 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.
Post:
adds new element to end of possibly extended vector

remove

public ELTTYPE remove(ELTTYPE element)
Remove an element, by value, from vector.

Specified by:
remove in interface List<ELTTYPE>
Specified by:
remove in interface Structure<ELTTYPE>
Parameters:
element - the element to be removed.
Returns:
the element actually removed, or if none, null.
Post:
element equal to parameter is removed and returned

capacity

public int capacity()
Determine the capacity of the vector. The capacity is always at least as large as its size.

Returns:
The size of the array underlying the vector.
Post:
returns allocated size of the vector

clone

public java.lang.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 java.lang.Object
Returns:
A copy of the original vector.
Post:
returns a copy of the vector, using same objects

contains

public boolean contains(ELTTYPE elem)
Determine if a value appears in a vector.

Specified by:
contains in interface List<ELTTYPE>
Specified by:
contains in interface Structure<ELTTYPE>
Overrides:
contains in class AbstractList<ELTTYPE>
Parameters:
elem - The value sought.
Returns:
True iff the value appears in the vector.
Post:
returns true iff Vector contains the value (could be faster, if orderedVector is used)

copyInto

public void copyInto(ELTTYPE[] 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().
Pre:
dest has at least size() elements
Post:
a copy of the vector is stored in the dest array

elementAt

public ELTTYPE 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.
Returns:
A reference to the value found in the vector.
Pre:
0 <= index && index < size()
Post:
returns the element stored in location index

get

public ELTTYPE get(int index)
Fetch the element at a particular index. The index of the first element is zero.

Specified by:
get in interface List<ELTTYPE>
Parameters:
index - The index of the value sought.
Returns:
A reference to the value found in the vector.
Pre:
0 <= index && index < size()
Post:
returns the element stored in location index

iterator

public java.util.Iterator<ELTTYPE> iterator()
Construct a iterator over the elements of the vector. The iterator considers elements with increasing index.

Specified by:
iterator in interface java.lang.Iterable<ELTTYPE>
Specified by:
iterator in interface List<ELTTYPE>
Specified by:
iterator in interface Structure<ELTTYPE>
Returns:
an iterator to traverse the vector.
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()
Post:
returns an iterator allowing one to view elements of vector

firstElement

public ELTTYPE firstElement()
Get access to the first element of the vector.

Returns:
Access to the first element of the vector.
Pre:
vector contains an element
Post:
returns first value in vector

indexOf

public int indexOf(ELTTYPE elem)
Assuming the data is not in order, find the index of a value, or return -1 if not found.

Specified by:
indexOf in interface List<ELTTYPE>
Parameters:
elem - The value sought in vector.
Returns:
The index of the first occurrence of the value.
Post:
returns index of element equal to object, or -1; starts at 0

indexOf

public int indexOf(ELTTYPE 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.
Returns:
The index of the first location, or -1 if not found.
Post:
returns index of element equal to object, or -1; starts at index

insertElementAt

public void insertElementAt(ELTTYPE 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.
Pre:
0 <= index <= size()
Post:
inserts new value in vector with desired index, moving elements from index to size()-1 to right

add

public void add(int index,
                ELTTYPE obj)
Insert an element at a particular location. Vector is grown as needed

Specified by:
add in interface List<ELTTYPE>
Parameters:
obj - the value to be inserted.
index - the location of the new value.
Pre:
0 <= index <= size()
Post:
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.

Specified by:
isEmpty in interface List<ELTTYPE>
Specified by:
isEmpty in interface Structure<ELTTYPE>
Overrides:
isEmpty in class AbstractList<ELTTYPE>
Returns:
True iff the vector is empty.
Post:
returns true iff there are no elements in the vector

lastElement

public ELTTYPE lastElement()
Fetch a reference to the last value in the vector.

Returns:
A reference to the last value.
Pre:
vector is not empty
Post:
returns last element of the vector

lastIndexOf

public int lastIndexOf(ELTTYPE obj)
Search for the last occurrence of a value within the vector. If none is found, return -1.

Specified by:
lastIndexOf in interface List<ELTTYPE>
Parameters:
obj - The value sought.
Returns:
The index of the last occurrence in the vector.
Post:
returns index of last occurrence of object in the vector, or -1

lastIndexOf

public int lastIndexOf(ELTTYPE 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.
Returns:
The index of the last occurrence of the value, or -1 if none.
Pre:
index >= 0
Post:
returns the index of last occurrence of object at or before index

clear

public void clear()
Remove all the values of the vector.

Specified by:
clear in interface List<ELTTYPE>
Specified by:
clear in interface Structure<ELTTYPE>
Post:
vector is empty

removeAllElements

public void removeAllElements()
Remove all the elements of the vector. Kept for compatibility with java.util.Vector.

See Also:
clear()
Post:
vector is empty

removeElementAt

public void removeElementAt(int where)
Remove an element at a particular location.

Parameters:
where - The location of the element to be removed.
Pre:
0 <= where && where < size()
Post:
indicated element is removed, size decreases by 1

remove

public ELTTYPE remove(int where)
Remove an element at a particular location.

Specified by:
remove in interface List<ELTTYPE>
Parameters:
where - The location of the element to be removed.
Returns:
value retrieved from location i (returns null if i invalid)
Pre:
0 <= where && where < size()
Post:
indicated element is removed, size decreases by 1

setElementAt

public void setElementAt(ELTTYPE 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.
Pre:
0 <= index && index < size()
Post:
element value is changed to obj

set

public ELTTYPE set(int index,
                   ELTTYPE obj)
Change the value stored at location index.

Specified by:
set in interface List<ELTTYPE>
Parameters:
obj - The new value to be stored.
index - The index of the new value.
Returns:
former value of ith entry of list.
Pre:
0 <= index && index < size()
Post:
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.
Pre:
newSize >= 0
Post:
vector is resized, any new elements are initialized

size

public int size()
Determine the number of elements in the vector.

Specified by:
size in interface List<ELTTYPE>
Specified by:
size in interface Structure<ELTTYPE>
Returns:
The number of elements within the vector.
Post:
returns the size of the vector

trimToSize

public void trimToSize()
Trim the vector to exactly the correct size.

Post:
minimizes allocated size of vector

toString

public java.lang.String toString()
Determine a string representation for the vector.

Overrides:
toString in class java.lang.Object
Returns:
A string representation for the vector.
Post:
returns a string representation of vector