我是新来的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>
哪里我做错?任何人都可以解释,请给我
预先感谢您
'GenericStack'没有'push'方法,只有嵌套的类'Stack'有它。 – toniedzwiedz 2013-02-24 17:07:56
因为没有接受int参数的方法,例如push(int)。 – 2013-02-24 17:09:10
我想你正在考虑[Autoboxing](http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html) – 2013-02-24 17:09:55