1

我是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>

我哪里做错了?!谁能给我解释一下

先感谢您

4

5 回答 5

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;
    }
}
于 2013-02-24T17:09:39.487 回答
2

因为方法push是在类中定义的GenericStack.Stack,而不是GenericStack. 为了使它工作更换

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

GenericStack<Integer>.Stack ob = new GenericStack.Stack ();
于 2013-02-24T17:14:18.820 回答
2

您的代码的主要问题是您混合了 2 个公共类,只是更改了一些代码,玩得开心!

通用堆栈.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);
        }

    }
}
于 2013-02-24T17:14:34.973 回答
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);
    }

  }
}

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

希望这可以帮助。

于 2013-02-24T17:22:45.033 回答
1

去掉类的额外Stack涂层GenericStack

谢谢!

于 2013-02-24T17:15:53.263 回答