MyArrayList MyArrayList MyArrayList MyArrayList MyArrayList MyArrayList MyArrayList MyArrayList MyArrayList MyArrayList
@param <E> */ public class MyArrayList<E> implements Iterable<E>{// Set the default capacity to 10 private static final int DEFAULT_CAPACITY = 10; Private static final Object[] DEFAULT_EMPTY_ELEMENTDATA = {}; Private int size; Private Object[] elementData; Public MyArrayList() {this.elementData = DEFAULT_EMPTY_ELEMENTDATA; Private void ensureCapacity(int minCapacity) {int minExpand = this.elementData! = DEFAULT_EMPTY_ELEMENTDATA ? 0 : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(minCapacity)); } private int calculateCapacity(int minCapacity) {if (this.elementData == DEFAULT_EMPTY_ELEMENTDATA) { return Math.max(minCapacity, DEFAULT_CAPACITY); } return minCapacity; } private void ensureExplicitCapacity(int minCapacity) { if (minCapacity > this.elementData.length) { grow(minCapacity); } } private void grow(int minCapacity) { int oldCapacity = this.elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity < minCapacity) { newCapacity = minCapacity; } if (newCapacity > Integer.MAX_VALUE - 8) { newCapacity = Integer.MAX_VALUE; } this.elementData = Arrays.copyOf(this.elementData, newCapacity); } public int size() { return this.size; } public int getLength() { return this.elementData.length; } public boolean isEmpty() { return size() == 0; Public Boolean add(E element) {ensureCapacityInternal(this.size + 1); this.elementData[this.size++] = element; return true; } // Add public void add(int index, E element) {ensureCapacityInternal(this.size + 1); System.arraycopy(this.elementData, index, this.elementData, index + 1, size - index); this.elementData[index] = element; size++; Public void clear() {for (int I = 0; i < this.size; i++) { this.elementData[i] = null; } this.size = 0; Public Boolean remove(element) {if (element == null) {for (int index = 0; index < this.size; index++) { if (this.elementData[index] == null) { remove(index); return true; } } } else { for (int index = 0; index < this.size; index++) { if (this.elementData[index].equals(element)) { remove(index); return true; } } } return false; } public E remove(int index) {E oldValue = (E)this.elementData[index]; int moveElementNum = this.size - 1 - index; if (moveElementNum > 0) { System.arraycopy(this.elementData, index + 1, this.elementData, index, moveElementNum); } this.elementData[--this.size] = null; return oldValue; } public E set(int index, E element) {E oldValue = (E)this.elementData[index]; this.elementData[index] = element; return oldValue; } public E get(int index) { return (E)this.elementData[index]; } @Override public String toString() { return Arrays.toString(this.elementData); } @Override public Iterator<E> iterator() { return new Itr(); } private class implements Iterator<E> {// implements Iterator; Int lastRet = -1; @Override public boolean hasNext() { return cursor ! = size; } @override public E next() {if (cursor >= size) {new Exception(" no element "); } if (cursor >= elementData.length) {new Exception(" array index exceeded "); } lastRet = cursor; return (E)elementData[cursor++]; } @Override public void remove() { MyArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; }}}Copy the code