1 Star 0 Fork 0

Roderland/algorithm

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MyArrayStack.java 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
Roderland 提交于 2020-10-23 18:19 +08:00 . add
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("栈空");
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Roderland/algorithm.git
git@gitee.com:Roderland/algorithm.git
Roderland
algorithm
algorithm
master

搜索帮助