// An implementation of Vectors, similar to that defined in java.utils // Implementation (c) 1996 duane a. bailey // Interface as defined in Java. //import java.util.Enumeration; /* Shortcomings of arrays Capacity to grow - big in modern programs error checking general processing checks Solution: Vector Flexible size Error checking additional processing Functions: array functionality constructor elementAt setElementAt size new stuff addElement (to end) insertElementAt (in middle) isEmpty removeElementAt removeElement capacity things we'll need ensureCapacity plus other junk Storage required elementData - the array elementCount - how many defaultCapacity - normal size capacityIncrement - how to grow it Do we need the length - answer is no, we can get it from the array */ public class Vector implements Cloneable { protected Object elementData[]; // the data protected int elementCount; // # of elts in vec protected int capacityIncrement; // the rate of growth for vector protected final static int defaultCapacity = 10; // def't capacity, must be>0 public Vector(int initialCapacity) // pre: initialCapacity >= 0 // post: constructs an empty vector with initialCapacity capacity { assert(initialCapacity >= 0); elementData = new Object[initialCapacity]; elementCount = 0; capacityIncrement = 0; } public Vector() // post: constructs an empty vector { this(defaultCapacity); // call one parameter constructor } public Vector(int initialCapacity, int capacityIncr) // pre: initialCapacity >= 0, capacityIncr >= 0 // post: constructs an empty vector with initialCapacity capacity // that extends capacity by capacityIncr, or doubles if 0 { assert(initialCapacity >= 0); elementData = new Object[initialCapacity]; elementCount = 0; capacityIncrement = capacityIncr; } public void ensureCapacity(int minCapacity) // post: the capacity of this vector is at least minCapacity. { if (elementData.length < minCapacity) { int newLength = elementData.length; // initial guess if (capacityIncrement == 0) { // increment of 0 suggests doubling (default) if (newLength == 0) newLength = 1; while (newLength < minCapacity) { newLength *= 2; } } else { // increment != 0 suggests incremental increase while (newLength < minCapacity) { newLength += capacityIncrement; } } // assertion: newLength > elementData.length. Object newElementData[] = new Object[newLength]; int i; // copy old data to array for (i = 0; i < elementCount; i++) { newElementData[i] = elementData[i]; } elementData = newElementData; // N.B. Garbage collector will pick up elementData } // assertion: capacity is at least minCapacity } public void addElement(Object obj) // post: adds new element to end of possibly extended vector { ensureCapacity(elementCount+1); elementData[elementCount] = obj; elementCount++; } public int capacity() // post: returns allocated size of the vector { return elementData.length; } public boolean contains(Object elem) // post: returns true iff Vector contains the value // (could be faster, if orderedVector is used) { int i; for (i = 0; i < elementCount; i++) { if (elem.equals(elementData[i])) return true; } return false; } public Object elementAt(int index) // pre: 0 <= index && index < size() // post: returns the element stored in location index { return elementData[index]; } //LT: i took this out so that it compiles (w/o structure package) // public Iterator elements() // // post: returns an iterator (ordered enumeration) allowing one to // // view elements of vector // { // return new VectorIterator(this); // } public void insertElementAt(Object obj, int index) // pre: 0 <= index <= size() // post: inserts new value in vector with desired index // moving elements from index to size()-1 to right { int i; ensureCapacity(elementCount+1); // N.B. must copy from right to left to avoid destroying data for (i = elementCount; i > index; i--) { elementData[i] = elementData[i-1]; } // assertion: i == index and element[index] is available elementData[index] = obj; elementCount++; } public boolean isEmpty() // post: returns true iff there are no elements in the vector { return size() == 0; } public boolean removeElement(Object element) // post: element equal to parameter is removed { int where = indexOf(element); if (where == -1) return false; removeElementAt(where); return true; } public int indexOf(Object obj) { //post: return the index s th vector[index].equals(obj) is //true; return -1 if not in the vector for (int i=0; i