package username.util; /** * A simple wrapper for simple arrays that permit the arrays to * expand dynamically. Arrays expand when you set a value at * a location larger than has been used previously. * * @author Samuel A. Rebelsky * @version 1.0 of April 2006 */ public class DynamicArray { // +------------------+------------------------------------- // | Design Decisions | // +------------------+ /* (1) We use a SimpleArray object, contents, as the underlying representation. (2) We use a helper, ensureMinimumSize(int min), to expand the underlying array when necessary. This helper should build a new SimpleArray of size at least min, copy over the values, and set contents to that new array. (3) When the user calls get() with a too-large index, we return null. */ // +--------+----------------------------------------------- // | Fields | // +--------+ /** The underlying array. */ SimpleArray contents; // +--------------+----------------------------------------- // | Constructors | // +--------------+ /** * Build a new array of specified size. */ public DynamicArray(int size) { this.contents = new SimpleArray(size); } // DynamicArray(int) /** * Build a new array, initializing the values to those * in stuff. */ public DynamicArray(T[] stuff) { this.contents = new SimpleArray(stuff); } // DynamicArray(T[]) // +------------------+------------------------------------- // | Standard Methods | // +------------------+ /** * Convert to a string for printing. */ public String toString() { return this.contents.toString(); } // toString() /** * This array equals another object if the other object is an * array and its contents are equal. */ @SuppressWarnings({"unchecked"}) public boolean equals(Object other) { try { return this.equals((DynamicArray) other); } catch (ClassCastException cce) { return false; } } // equals(Object) /** * This array equals another dynamic array if the underlying * SimpleArray objects are qual. */ public boolean equals(DynamicArray other) { return this.contents.equals(other.contents); } // equals(DynamicArray) /** * Generate a hash code. */ public int hashCode() { return this.contents.hashCode(); } // hashCode // +----------------+--------------------------------------- // | Public Methods | // +----------------+ /** * Get the value at position i. * * @return t * the value at index i. * * @pre * 0 <= i * @post * t is a value that meets the criertion that the most recent call * to set with first parameter equal to i has a second parameter * equal to t. * @post * If no call to set with equal i has been made, returns null. */ public T get(int i) { if (i < this.size()) { return this.contents.get(i); } else { return null; } } // get(int) /** * Set the value at index i to newval. * * @pre * 0 <= i < 'the size of the largest array this machine will permit' */ public void set(int i, T newval) { this.ensureMinimumSize(i+1); this.contents.set(i,newval); } // set(int, T) /** * Get the size of this array. */ public int size() { return this.contents.size(); } // size() // +-----------------+-------------------------------------- // | Private Methods | // +-----------------+ /** * Ensure that contents has size at least min. * * @pre * 0 <= min * @post * this.size() >= min */ void ensureMinimumSize(int min) { // STUB } // ensureMinimumSize(int) } // class DynamicArray