2013-02-24 64 views
1

我是新来的Java中的泛型,我真的需要帮助这个代码 它不编译我不知道为什么!
Stack类是:为什么不是这个java代码工作?!通用堆栈

public class GenericStack<Item>{ 
    public class Stack { 

     private Node first=null; 

     private class Node { 
      Item item; 
      Node next; 
     } 

     public boolean IsEmpty() 
     { 
      return first==null; 
     } 

     public void push (Item item) 
     { 
      Node oldfirst = first; 
      first = new Node(); 
      first.item = item; 
      first.next = oldfirst; 
     } 

     public Item pop() 
     { 
      Item item=first.item; 
      first=first.next; 
      return item; 
     } 
    } 
} 

,这里是主要的

public class Main { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    GenericStack<Integer> ob = new GenericStack<Integer>(); 
    ob.push(5); 
    obpush(10); 
    ob.push(15); 
    while (!ob.IsEmpty()) 
    { 
     int x=ob.pop(); 
     StdOut.print(x); 
    } 

    } 
} 

现在的错误是:

The method push(int) isn't defined for the type GenericStack<Integer> 

哪里我做错?任何人都可以解释,请给我

预先感谢您

+3

'GenericStack'没有'push'方法,只有嵌套的类'Stack'有它。 – toniedzwiedz 2013-02-24 17:07:56

+0

因为没有接受int参数的方法,例如push(int)。 – 2013-02-24 17:09:10

+0

我想你正在考虑[Autoboxing](http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html) – 2013-02-24 17:09:55

回答

2
class GenericStack<Item>{ 
    class Stack { 

     private Node first=null; 

     private class Node { 
      Item item; 
      Node next; 
     } 

     public boolean IsEmpty() 
     { 
      return first==null; 
     } 

     public void push (Item item) 
     { 
      Node oldfirst = first; 
      first = new Node(); 
      first.item = item; 
      first.next = oldfirst; 
     } 

     public Item pop() 
     { 
      Item item=first.item; 
      first=first.next; 
      return item; 
     } 
    } 
} 

public class Main { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    GenericStack<Integer> ob = new GenericStack<Integer>(); 
    GenericStack<Integer>.Stack st=ob.new Stack(); 
    st.push(5); 
    st.push(10); 
    st.push(15); 
    while (!st.IsEmpty()) 
    { 
     int x=st.pop(); 
//  StdOut.print(x); 
     System.out.println(x); 
    } 

    } 
} 

您正在调用内部类的方法。因此,使用外部类的对象不能直接调用内部类的方法。请参阅上面的代码。

希望这会有所帮助。

3

GenericStack类没有方法。摆脱嵌套类结构的,并使用一般类型参数为Stack直接:

public class Stack<Item> { 

    private Node first=null; 

    private class Node { 
     Item item; 
     Node next; 
    } 

    public boolean IsEmpty() 
    { 
     return first==null; 
    } 

    public void push (Item item) 
    { 
     Node oldfirst = first; 
     first = new Node(); 
     first.item = item; 
     first.next = oldfirst; 
    } 

    public Item pop() 
    { 
     Item item=first.item; 
     first=first.next; 
     return item; 
    } 
} 
+0

天啊!这很简单。谢谢先生 – Coderji 2013-02-24 17:15:08

2

由于方法pushGenericStack.Stack,不GenericStack被定义。为了使工作更换

GenericStack<Integer> ob = new GenericStack<Integer>(); 

GenericStack<Integer>.Stack ob = new GenericStack.Stack(); 
+0

由于它不是一个静态的内部类,你需要有一个封闭类的实例。所以你的第二个陈述应该是GenericStack .Stack st = outerClassObject.new Stack(); – ankurtr 2013-02-24 17:24:54

2

的主要问题与您的代码是你混合2个公开课,只是改变了一点点你的代码,有乐趣!

GenericStack.java

public class GenericStack<Item> { 

    private Node first = null; 

    private class Node { 
     Item item; 
     Node next; 
    } 

    public boolean IsEmpty() { 
     return first == null; 
    } 

    public void push(Item item) { 
     Node oldfirst = first; 
     first = new Node(); 
     first.item = item; 
     first.next = oldfirst; 
    } 

    public Item pop() { 
     Item item = first.item; 
     first = first.next; 
     return item; 
    } 

} 

TestGenericStack.java

public class TestGenericStack { 

    public static void main(String[] args) { 

     GenericStack<Integer> ob = new GenericStack<Integer>(); 
     ob.push(5); 
     ob.push(10); 
     ob.push(15); 
     while (!ob.IsEmpty()) { 
      int x = ob.pop(); 
      System.out.println(x); 
     } 

    } 
} 
1

得到GenericStack摆脱Stack类的额外涂层。

谢谢!

+0

这是不必要的。毕竟,Java中有[autoboxing](http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html)这样的东西。 – 2013-02-24 17:23:48

+0

感谢您的更正... :) – 2013-02-24 17:31:13

+0

这不仅是不必要的,而且它也浪费了内存。 'new Integer(5)'创建一个新的对象,而'Integer.valueOf(5)'或者只是'5'都允许重用现有的'Integer'实例。 – toniedzwiedz 2013-02-24 18:11:59

相关问题