Class ResizableArray

java.lang.Object
  extended by ResizableArray

public class ResizableArray
extends java.lang.Object


Field Summary
static int DEFAULT_SIZE
           
 
Constructor Summary
ResizableArray()
          Creates a resizable array with DEFAULT_SIZE elements.
ResizableArray(int size)
          Creates a resizable array with the given number of elements.
 
Method Summary
 void add(java.lang.Object o)
          Adds the given object to the end of this ResizableArray, resizing the underlying array if necessary.
 int capacity()
          Returns the capacity of this array.
 java.lang.Object get(int pos)
          Returns the element at the given index.
 void insertBefore(java.lang.Object o, int pos)
          Inserts an element before the element at the given position and increases the size of this array.
 boolean isEmpty()
           
 void removeElementAt(int pos)
          Removes the element at the given index, shifting all subsequent elements forward and decreasing the size of this array by one.
 void set(int pos, java.lang.Object o)
          Sets the element at the give index.
 int size()
          Returns the size of this array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SIZE

public static final int DEFAULT_SIZE
See Also:
Constant Field Values
Constructor Detail

ResizableArray

public ResizableArray()
Creates a resizable array with DEFAULT_SIZE elements. (You should declare a class constant called DEFAULT_SIZE with the initial value 5.)


ResizableArray

public ResizableArray(int size)
Creates a resizable array with the given number of elements.

Parameters:
size -
Method Detail

capacity

public int capacity()
Returns the capacity of this array. The capacity of this array is the length of the underlying array, and corresponds to the maximum size that this ResizableArray can contain before the undelying array must be resized.

Returns:
the capacity of this array

size

public int size()
Returns the size of this array. The size of a ResizableArray is the number of elements it actually contains.

Returns:
the size of this array

isEmpty

public boolean isEmpty()
Returns:
true if this array has a size of 0, false otherwise

removeElementAt

public void removeElementAt(int pos)
Removes the element at the given index, shifting all subsequent elements forward and decreasing the size of this array by one.

Parameters:
pos - the index of the element to remove.

insertBefore

public void insertBefore(java.lang.Object o,
                         int pos)
Inserts an element before the element at the given position and increases the size of this array. This method must resize the underlying array if necessary (i.e. if capacity() == size()), and must shift all elements from pos through size() "back" in the array.

Parameters:
o - the object to insert
pos - the index where pos is to be inserted.

get

public java.lang.Object get(int pos)
Returns the element at the given index. Analogous to a[pos] in Java language arrays. Precondition: 0 <= pos < this.size();

Parameters:
pos - the array position
Returns:
the Object at that position. (Note that you will have to cast this object to something useful.)

set

public void set(int pos,
                java.lang.Object o)
Sets the element at the give index. Analogous to a[pos] = o; in Java language arrays. Precondition: 0 <= pos < this.size();

Parameters:
o - the object to give to element pos of this array
pos - the element to update

add

public void add(java.lang.Object o)
Adds the given object to the end of this ResizableArray, resizing the underlying array if necessary.

Parameters:
o - the Object to add