ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
```java package ds.impl; import ds.Stack; import java.util.Iterator; import ds.util.ArrayIterator; public class ArrayStack<T> implements Iterable<T>,Stack<T> { private T[] array; private int size; public ArrayStack(){ array = (T[]) new Object[2]; size = 0; } @Override public Iterator<T> iterator() { return new ArrayIterator(array,size); } @Override public int size() { return size; } @Override public void push(T data) { if(size == array.length){ resize(2* array.length); } array[size++] = data; } private void resize(int capacity) { assert capacity >= size; T[] tmp = (T[]) new Object[capacity]; System.arraycopy(array, 0, tmp, 0, size); array = tmp; } @Override public T pop() { notEmptyCheck(); T result = array[size-1]; array[size-1] = null; size--; if(size>0 && size == array.length/4){ resize(array.length/2); } return result; } @Override public T peek() { notEmptyCheck(); return array[size-1]; } } ```