1 Star 0 Fork 0

20162319莫礼钟 / mlz20162319Supplement

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ArrayStack.java 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
hasee 提交于 2017-10-10 00:08 . ArrayStack方法实现
import java.text.DecimalFormat;
import java.util.EmptyStackException;
/**
* Created by Mose on 2017/10/9.
*/
public class ArrayStack<T> implements Stack<T> {
private final int DEFAULT_CAPACITY = 10;
private int count;
private T[] stack;
public ArrayStack() {
count = 0;
stack = (T[]) (new Object[DEFAULT_CAPACITY]);
}
public void push(T element) {
if (count == stack.length)
expandCapacity();
stack[count] = element;
count++;
}
public String toString() {
String result = "<top of stack>\n";
for (int index = count - 1; index >= 0; index--)
result += stack[index] + "\n";
return result + "<bottom of stack>";
}
private void expandCapacity() {
T[] larger = (T[]) (new Object[stack.length * 2]);
for (int index = 0; index < stack.length; index++)
larger[index] = stack[index];
stack = larger;
}
public int size() {
return count;
}
public boolean isEmpty() {
if (count == 0)
return true;
else
return false;
}
public T peek() throws EmptyStackException {
if (count==0)
System.out.println("Error.");
return stack[count - 1];
}
public T pop() throws EmptyStackException {
if (count==0)
System.out.println("Error.");
T result = stack[count-1];
count = count-1; //count--
stack[count]=null;
return result;
}
}
Java
1
https://gitee.com/Mosemonkey/mlz20162319supplement.git
git@gitee.com:Mosemonkey/mlz20162319supplement.git
Mosemonkey
mlz20162319supplement
mlz20162319Supplement
master

搜索帮助