【发布时间】:2018-04-30 03:53:16
【问题描述】:
如何将下面的类改成使用ArrayList和LinkedList的类?我必须更改此类中的所有代码。
我对 ArrayList 和 LinkedList 感到困惑
import java.util.ArrayList;
public class ArrayStack {
private int maxsize;
private int top;
private int[] items;
public ArrayStack(int maxsize) {
if (maxsize <= 0)
throw new ArrayStackException("Stack size must be positive");
items = new int[maxsize];
this.maxsize = maxsize;
top = 0;
}
public void push(int item) {
if (top == items.length)
throw new ArrayStackException("Overflow Error");
items[top]=item;
top++;
}
public int pop() {
if (isEmpty())
throw new ArrayStackException("Underflow Error");
return items[--top];
}
public boolean isEmpty() {
return (top==0);
}
public static class ArrayStackException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public ArrayStackException(String message) {
super(message);
}
}
public static void main (String[] a) {
ArrayStack stack = new ArrayStack(3);
stack.push(1);
stack.push(2);
stack.push(3);
// srtack.push(4); //over flow error
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
给我一些关于这个问题的解释。
【问题讨论】:
-
implements ArrayList是什么意思,ArrayList不是接口。 -
我的意思是在 java 上使用了 ArrayList
-
你到底有什么不明白的?我们不是来帮你做作业的
-
我不了解java上的ArrayList和LinkedList
-
也许这段代码可以解释我的问题
标签: java arraylist linked-list