2011-08-22 67 views
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和集合很不满意,并且在提交任务时已经很晚了。 任何帮助/方向将不胜感激。

+0

我想你的意思是写ISortableStack S =新SortableStack();你在这里没有泛型的问题,你不能实例化一个接口。你只能实例化一个ISortableStack的实现。 –

+0

我知道这是一个Q + A网站,任何人都可以自由地提出问题,但是您不会通过让其他人解决您的任务来学习如何编程。我希望你从这些答案中学习。 –

+1

感谢您回复Dru。但是,我比其他任何教科书都能从中学到更多。感谢@ZenMaster,@ Mark Peters,甚至你给予了这样的真知灼见。至于做分配的事情,我很高兴,至少我试图弄清楚自己。 – Yonathan

回答

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

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

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

现在,ESortableStack类型参数:这对一些特定一个占位符,就像String。您不需要指定E作为该类的用户,您需要告诉编译器E应该映射到此实例。它看起来像你的筹码需要持有字符,所以你真正想要的是:

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

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

你不需要ch是的demo成员。

+0

打我吧:) @Yonathan除了这个,你会遇到一个问题,因为你没有一个构造函数接受'int'。 – Shaded

+0

@Shaded:这是一个很好的观点,我现在要改变它。 –

+0

我确定,我会的。任何建议来改进这个代码? – Yonathan

0

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

让我们对它们进行排序逐一:

ISortableStack是原始类型。参考通用类型 ISortableStack应参数化

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

无法实例ISortableStack

您正试图创建一个接口的实例的类型 - 这当然是应该失败的。改为使用班级。

不能使静态参考非静态类型E

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

除此之外, - 你似乎缺少代码的部分...