1

我正在使用以下方法创建此堆栈类,如图所示。

 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 和集合,并且在提交作业时已经很晚了。任何帮助/方向将不胜感激。

4

2 回答 2

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

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

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

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

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

char character;
while ( (character = (char)System.in.read()) != '\n') {
   //...
   s.push(character);
}

您无需ch成为demo.

于 2011-08-22T19:33:52.633 回答
0

在该特定行 ( ISortableStack<E> s = new ISortableStack(5);) 发生了几件事。

让我们一一整理:

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

这里的问题是您正在尝试使用原始类型。下一步是对其进行参数化:

无法实例化类型 ISortableStack

您正在尝试创建接口的实例 - 这当然应该失败。改用一个类。

无法对非静态类型 E 进行静态引用

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

除此之外-您似乎缺少部分代码...

于 2011-08-22T19:44:55.463 回答