代码拉取完成,页面将自动刷新
package Stack;
/*
@author: Roderland
@create: 2020-08-10---21:12
*/
/*
自编数组实现简单栈
*/
public class MyArrayStack<E> {
private static final int DEFAULT_CAPACITY = 10;
private E[] array;
private int elementCount = 0;
public MyArrayStack(int capacity) { array = (E[])new Object[capacity]; }
public MyArrayStack() { array = (E[])new Object[DEFAULT_CAPACITY]; }
public void push(E o) {
if (elementCount >= array.length) expansion();
array[elementCount++] = o;
}
private void expansion() {
E[] newArray = (E[]) new Object[array.length*2];
//System.arraycopy(array, 0, newArray, 0, array.length);
for (int i = 0; i < array.length; i++) {
newArray[i] = array[i];
}
array = newArray;
}
public E peek() {
EmptyCheck();
return array[elementCount-1];
}
public E pop() {
EmptyCheck();
return array[--elementCount];
}
public boolean isEmpty() {
return elementCount == 0;
}
public int size() {
return elementCount;
}
private void EmptyCheck() {
if (isEmpty())
throw new IndexOutOfBoundsException("栈空");
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。