size();
/*
Return number of elements in this list.
*/
size() {
return this._size;
}
get(index);
/*
Return element at given index.
If passed arg "index" is invalid, throws exception:
"(get) Index %d is out of bounds"
*/
get(index) {
if (index < 0 || index >= this.size())
throw '(get) Attempt to access element in empty list';
return this.fixedArray[index];
}
add(newElem);
/*
Alias for "addLast" (same functionality).
*/
add(newElem) {
this.addLast(newElem);
}
addLast(newElem);
/*
Add (append) the passed arg "newElem" to end of list
All existing elements are preserved and will precede (be before) "newElem".
No return value
Example:
given "this" dynamic list is [10, 20, 30, 40]
and "(newElem)" is (1002)
Then, after "addLast" is finished, the result:
"this" dynamic list is [10, 20, 30, 40, 1002]
*/
addLast(newElem) { //Add the passed arg "newElem" to end of list if (!this.hasCapacity()) this.grow(); this.fixedArray[this.size()] = newElem; this._size++; }
hasCapacity() { // Return true if our capacity is greater than our size // i.e. if we have excess capacity return this.getCapacity() > this.size(); } getCapacity() { return this.fixedArray.length; }
grow() { // Grow (increase) our capacity by factoring our //current capacity by the input param "growthFactor" // In this method var "fa" will be the new fixed array this.growCapacityTo((int)Math.round(getCapacity() * this.growthFactor)); } growCapacityTo(newCapacity) { // Grow (increase) our capacity to "newCapacity" // Construct new fixed array // Copy existing elements into new fixed array let oldFixedArray = this.fixedArray; let newFixedArray = this.newFixedArray(newCapacity); //Copy for (let i = 0; i < this.size(); i++) newFixedArray[i] = oldFixedArray[i]; this.fixedArray = newFixedArray; }