The complete code
class ArrayList<E> {
/** * The number of elements */
private sizeNum:number = 0;
/** * all elements */
private elements:E[] = [];
private static readonly DEFAULT_CAPACITY = 10;
private static readonly ELEMENT_NOT_FOUND = -1;
constructor(capaticy? :number) {
capaticy = (capaticy < ArrayList.DEFAULT_CAPACITY) ? ArrayList.DEFAULT_CAPACITY : capaticy;
this.elements = new Array<E>(capaticy);
}
/** * clears all elements */
public clear() {
for (let i = 0; i < this.sizeNum; i++) {
this.elements[i] = null;
}
this.sizeNum = 0;
}
/** * gets the number of elements *@return* /
public size():number {
return this.sizeNum;
}
/** * check whether the value is null *@return* /
public isEmpty():boolean {
return this.sizeNum === 0;
}
/** * Whether to contain an element *@return* /
public contains(element:E):boolean {
return this.indexOf(element) ! = ArrayList.ELEMENT_NOT_FOUND; }/** * add the element to the end *@param element* /
public add(element:E) {
this.addToIndex(this.sizeNum, element);
}
/** * gets the element * at index position@param index* /
public get(index:number):E {
this.rangeCheck(index);
return this.elements[index];
}
/** * Sets the element * at index position@param index
* @param element* /
public set(index:number.element:E):E {
this.rangeCheck(index);
const old:E = this.elements[index];
this.elements[index] = element;
return old;
}
/** * inserts an element * at index position@param index
* @param element* /
public addToIndex(index:number, element:E) {
this.rangeCheckAdd(index);
this.ensureCapacity(index + 1);
for (let i = this.sizeNum - 1; i > index; i--) {
this.elements[i] = this.elements[i - 1];
}
this.elements[index] = element;
this.sizeNum++;
}
/** * delete element * at index position@param index* /
public remove(index:number):E {
this.rangeCheck(index);
const oldElement:E = this.elements[index];
for (let i = this.sizeNum; i > index; i--) {
this.elements[i] = this.elements[i - 1];
}
this.sizeNum--;
return oldElement;
}
/** * View the index of the element *@param element* /
public indexOf(element:E):number {
return ArrayList.ELEMENT_NOT_FOUND;
}
private outOfBounds(index:number) {
throw new Error(`ArrayIndexOutOfBoundsException: ${index}`);
}
private rangeCheck(index:number) {
if (index < 0 || index >= this.sizeNum) {
this.outOfBounds(index); }}private rangeCheckAdd(index:number) {
if (index < 0 || index > this.sizeNum) {
this.outOfBounds(index); }}/** * ensure that there is a capacity **@param capacity* /
public ensureCapacity(capacity:number) {
const oldCapacity = this.elements.length;
if (oldCapacity > capacity) return;
const newCapacity = oldCapacity + (oldCapacity >> 1);
const newElements:E[] = new Array<E>(newCapacity);
for (let i = 0; i< this.sizeNum; i++) {
newElements[i] = this.elements[i];
}
this.elements = newElements;
}
public toString():string {
let str:string = ' ';
str += `size=The ${this.sizeNum}[`;
for (let i = 0; i < this.sizeNum; i++) {
if(i ! = =0) {
str += ', ';
}
str += this.elements[i];
}
str += '] '
returnstr; }}export default ArrayList;
Copy the code