我正在使用以下方法创建此堆栈类,如图所示。
import java.util.ArrayList;
import java.util.EmptyStackException;
public class SortableStack<E extends Comparable<E>> implements ISortableStack<E> {
private int N;
private Node first;
private class Node {
private E e;
private Node next;
}
public SortableStack() {
first = null;
N = 0;
}
private ArrayList<E> listOne = new ArrayList<E>();
public boolean isEmpty() {
return first == null;
}
public int size() {
return N;
}
public void push(E e) {
Node oldfirst = first;
first = new Node();
first.e = e;
first.next = oldfirst;
N++;
}
public E pop() {
if (isEmpty()) throw new RuntimeException("Stack underflow");
E e = first.e; // save e to return
first = first.next; // delete first node
N--;
return e; // return the saved e
}
public E peekMidElement() {
if(listOne.size() <= 0){
throw new EmptyStackException();
}
return listOne.get(listOne.size()/2);
}
public E peekHighestElement() {
if(listOne.size() <= 0){
throw new EmptyStackException();
}
return listOne.get(listOne.size() - 1);
}
public E peekLowestElement() {
if(listOne.size() <= 0){
throw new EmptyStackException();
}
return listOne.get(0);
}
}`
//接口ISortableStack是[这里][1](注释描述了所需的方法签名)。
[1]:http://stackoverflow.com/questions/7130901/java-stack-implementation
现在,当我尝试创建主体类时,如下所示:
import java.io.*;
public class ExhibitStack<E extends Comparable<E> > {
E ch;
public static void main(String[] args) throws IOException {
ISortableStack<E> s = new ISortableStack(5); // Cannot instatiate ISORTABLESTACK
ExhibitStack demo = new ExhibitStack();
// Cannot make reference to a non static type
while ((demo.ch = (E) System.in.read()) != '\n') {
if (!s.full()) {
s.push(demo.ch);
}
}
while (!s.empty()) {
System.out.print(s.pop());
}
System.out.println();
}
}
它在 ISortableStack 处引发错误:无法引用非静态类型。并且无法启动 ISORTABLESTACK
我想使用界面创建菜单驱动程序。我不擅长 Java GENERICS 和集合,并且在提交作业时已经很晚了。任何帮助/方向将不胜感激。