class DynamicArray
//Returns a new empty DynamicArray //with defaults initCapacity=10 and growthFactor=2 static newEmpty() //Returns a new empty DynamicArray using passed parameters static fromCapacityGrowthFactor(anInitialCapacity, aGrowthFactor)
DynamicArray | +------- fixedArray ------- a fixed array | +------- growthFactor -- a floating point number | +------- _size -- an integer | +------- initialCapacity -- an integer
class DynamicArray { }
class DynamicArray {
/*
fixedArray - a fixed array
growthFactor - a floating point number
size - an integer
initialCapacity - an integer
*/
}
//Returns a new empty DynamicArray static newEmpty() { //TODO } static fromCapacityGrowthFactor(anInitCapacity, aGrowthFactor) { //TODO }
//-------------------------------------------------------------- //Public Constructors //Returns a new empty DynamicArray //with defaults initCapacity=10 and growthFactor=2 static newEmpty() { //Use defaults return new DynamicArray(10, 2); } static fromCapacityGrowthFactor(anInitialCapacity, aGrowthFactor) { return new DynamicArray(anInitialCapacity, aGrowthFactor); } //-------------------------------------------------------------- //Private (Classic) Constructors constructor(anInitialCapacity, aGrowthFactor) { this.initialCapacity = anInitialCapacity; this.growthFactor = aGrowthFactor; this.reset(); } //-------------------------------------------------------------- //Helpers reset() { this._size = 0; this.fixedArray = this.newFixedArray(this.initialCapacity); } newFixedArray(aSize) { //Varies per coding language (see next section for examples) //Construct fixed array with null elements, and with size "aSize" //Null elements are "blank" spots where "actual" elements will be placed //TODO }
private DynamicArray(int initCapacity, double aGrowthFactor)
newFixedArray(aSize) { //Construct fixed array with null elements, and with size "aSize" //Null elements are "blank" (available) spots where "real" elements will be placed let fixedArray = Array(aSize); // Fill with nulls fixedArray.fill(null); // Assure it is fixed Object.seal(fixedArray); return fixedArray; }
@SuppressWarnings("unchecked") private E[] newFixedArray(int aSize) { return (E[]) new Object[aSize]; }