moka.util
Class Vector

java.lang.Object
  |
  +--moka.lang.Object
        |
        +--moka.util.Vector

public class Vector
extends Object

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.

Since:
MDK1.0a

Field Summary
 int capacity
          The actual capacity of the Vector.
 int capacityIncrement
          The capacity increment of the Vector.
 int size
          The actual size of the Vector.
 
Constructor Summary
Vector()
          Creates a new Vector with an initial capacity of 10 and a capacity increment of 5.
Vector(moka.util.Enumeration enu)
          Creates a new Vector adding the elements of the specified enumeration.
Vector(int initialCapacity, int capacityIncrement)
          Creates a new Vector with the specified capacity and capacity increment.
 
Method Summary
 void add(int index, moka.lang.Object o)
          Inserts the specified element at the specified position in this vector.
 void clear()
          Removes all of the elements from this vector.
 boolean contains(moka.lang.Object o)
          Returns true if this vector contains the specified element.
 void copyInto(moka.lang.Object[] anArray, int fromIndex, int toIndex)
          Copies the components of this vector into the specified array.
 moka.lang.Object dequeue()
          Removes the object at the beginning of this queue and returns that object as the value of this function.
 moka.util.Enumeration elements()
          Returns an enumeration of the components of this vector.
 void enqueue(moka.lang.Object item)
          Enqueues an item at the end of this queue.
 void finalize()
          Frees the memory used by this object.
 moka.lang.Object get(int index)
          Returns the element at the specified position in this vector.
 int indexOf(moka.lang.Object o)
          Returns the index in this vector of the first occurrence of the specified element, or -1 if this list does not contain this element.
 int indexOf(moka.lang.Object elem, int index)
          Searches for the first occurence of the given argument, beginning the search at index.
 int lastIndexOf(moka.lang.Object o)
          Returns the index in this vector of the last occurrence of the specified element, or -1 if this list does not contain this element.
 int lastIndexOf(moka.lang.Object elem, int index)
          Searches backwards for the specified object, starting from the specified index, and returns an index to it.
 moka.lang.Object peek()
          Looks at the object at the top of this stack without removing it from the stack.
 moka.lang.Object pop()
          Removes the object at the top of this stack and returns that object as the value of this function.
 void push(moka.lang.Object item)
          Pushes an item onto the top of this stack.
 moka.lang.Object remove(int index)
          Removes the element at the specified position in this vector.
 boolean remove(moka.lang.Object o)
          Removes the first occurrence in this vector of the specified element.
 moka.lang.Object set(int index, moka.lang.Object element)
          Replaces the element at the specified position in this vector with the specified element.
 void setCapacity(int newCapacity)
          Sets the capacity of this vector.
 java.lang.String toString()
          Returns a string representation of this Vector, containing the String representation of each element.
 void trimToSize()
          Trims the capacity of this vector to be the vector's current size.
 
Methods inherited from class moka.lang.Object
equals, getClassName
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

capacity

public int capacity
The actual capacity of the Vector. Should be only read.


capacityIncrement

public int capacityIncrement
The capacity increment of the Vector. Sould be >= 1.


size

public int size
The actual size of the Vector. Should be only read.

Constructor Detail

Vector

public Vector()
Creates a new Vector with an initial capacity of 10 and a capacity increment of 5.


Vector

public Vector(int initialCapacity,
              int capacityIncrement)
Creates a new Vector with the specified capacity and capacity increment.

Parameters:
initialCapacity - The initial capacity.
capacityIncrement - The capacity increment.

Vector

public Vector(moka.util.Enumeration enu)
Creates a new Vector adding the elements of the specified enumeration.

Method Detail

elements

public moka.util.Enumeration elements()
Returns an enumeration of the components of this vector. The returned Enumeration object will generate all items in this vector. The first item generated is the item at index 0, then the item at index 1, and so on.

Returns:
an enumeration of the components of this vector.

add

public void add(int index,
                moka.lang.Object o)
Inserts the specified element at the specified position in this vector. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index - index at which the specified element is to be inserted.

push

public void push(moka.lang.Object item)
Pushes an item onto the top of this stack.

Parameters:
item - the item to be pushed onto this stack.

enqueue

public void enqueue(moka.lang.Object item)
Enqueues an item at the end of this queue.

Parameters:
item - the item to be enqueued.

clear

public void clear()
Removes all of the elements from this vector. This vector will be empty after this call returns.


finalize

public void finalize()
Frees the memory used by this object.

Overrides:
finalize in class Object

contains

public boolean contains(moka.lang.Object o)
Returns true if this vector contains the specified element.

Parameters:
o - element whose presence in this vector is to be tested.
Returns:
true if this vector contains the specified element.

get

public moka.lang.Object get(int index)
Returns the element at the specified position in this vector.

Parameters:
index - index of element to return.
Returns:
the element at the specified position in this vector.

indexOf

public int indexOf(moka.lang.Object o)
Returns the index in this vector of the first occurrence of the specified element, or -1 if this list does not contain this element.

Parameters:
o - element to search for.
Returns:
the index in this vector of the first occurrence of the specified element, or -1 if this vector does not contain this element.

lastIndexOf

public int lastIndexOf(moka.lang.Object o)
Returns the index in this vector of the last occurrence of the specified element, or -1 if this list does not contain this element.

Parameters:
o - element to search for.
Returns:
the index in this vector of the last occurrence of the specified element, or -1 if this vector does not contain this element.

remove

public moka.lang.Object remove(int index)
Removes the element at the specified position in this vector. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the vector.

Parameters:
index - the index of the element to removed.
Returns:
the element previously at the specified position.

remove

public boolean remove(moka.lang.Object o)
Removes the first occurrence in this vector of the specified element. If this vector does not contain the element, it is unchanged.

Parameters:
o - element to be removed from this vector, if present.
Returns:
true if this vector contained the specified element.

peek

public moka.lang.Object peek()
Looks at the object at the top of this stack without removing it from the stack.

Returns:
the object at the top of this stack (the last item of the Vector object).

pop

public moka.lang.Object pop()
Removes the object at the top of this stack and returns that object as the value of this function.

Returns:
The object at the top of this stack (the last item of the Vector object).

dequeue

public moka.lang.Object dequeue()
Removes the object at the beginning of this queue and returns that object as the value of this function.

Returns:
The object at the beginning of this queue (the first item of the Vector object).

set

public moka.lang.Object set(int index,
                            moka.lang.Object element)
Replaces the element at the specified position in this vector with the specified element.

Parameters:
index - index of element to replace.
element - element to be stored at the specified position.
Returns:
the element previously at the specified position.

copyInto

public void copyInto(moka.lang.Object[] anArray,
                     int fromIndex,
                     int toIndex)
Copies the components of this vector into the specified array. The array must be big enough to hold all the objects copied from this vector.

Parameters:
anArray - the array into which the components get copied.
fromIndex - the index of the first object in the vector to copy.
toIndex - the index after the last object in the vector to copy.

indexOf

public int indexOf(moka.lang.Object elem,
                   int index)
Searches for the first occurence of the given argument, beginning the search at index.

Parameters:
elem - an object.
index - the index to start searching from.
Returns:
the index of the first occurrence of the object argument in this vector at position index or later in the vector or -1 if the object is not found.

lastIndexOf

public int lastIndexOf(moka.lang.Object elem,
                       int index)
Searches backwards for the specified object, starting from the specified index, and returns an index to it.

Parameters:
elem - the desired component.
index - the index to start searching from.
Returns:
the index of the last occurrence of the specified object in this vector at position less than or equal to index in the vector or return -1 if the object is not found.

setCapacity

public void setCapacity(int newCapacity)
Sets the capacity of this vector.

Parameters:
newCapacity - the new size of this vector.

toString

public java.lang.String toString()
Returns a string representation of this Vector, containing the String representation of each element.

Overrides:
toString in class Object
Returns:
a string representation of the object.

trimToSize

public void trimToSize()
Trims the capacity of this vector to be the vector's current size. An application can use this operation to minimize the storage of a vector.