通用堆栈方法



我使用如下所示的方法创建这个堆栈类。

 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泛型和集合,提交作业也很晚。如有任何帮助/指示,我将不胜感激。

ISortableStack<E> s = new ISortableStack(5); //Cannot instatiate ISORTABLESTACK

ISortableStack是一个接口(它指定了方法的签名,但没有指定这些方法中的代码),因此它本身不能被实例化。相反,请尝试使用具体的实现类:

ISortableStack<E> s = new SortableStack<E>();

现在,SortableStack中的E类型参数:它是一些特定的类的占位符,如String。您不需要将E指定为该类的用户,而是需要告诉编译器E应该映射到这个实例的什么。看起来你的堆栈需要保存字符,所以你真正想要的是:

ISortableStack<Character> s = new SortableStack<Character>();
char character;
while ( (character = (char)System.in.read()) != 'n') {
   //...
   s.push(character);
}

不需要chdemo的成员

在特定的行(ISortableStack<E> s = new ISortableStack(5);)中,有几件事情正在进行。

让我们逐一整理:

ISortableStack是一个原始类型。引用泛型类型ISortableStack应该参数化

这里的问题是您试图使用原始类型。下一步是将其参数化:

不能实例化类型ISortableStack

你正在尝试创建一个接口的实例——这当然应该失败。使用类代替。

不能对非静态类型E

进行静态引用

类型参数不能在任何静态上下文中使用,您的main方法就是静态上下文中。

除此之外,你似乎遗漏了部分代码…

最新更新