1

私は Java の Generics が初めてで、このコードで本当に助けが必要です。コンパイルされていません。理由はわかりません。
スタック クラスは次のとおりです。

 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.StackGenericStack

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

GenericStack<Integer>.Stack ob = new GenericStack.Stack ();
于 2013-02-24T17:14:18.820 に答える
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);
        }

    }
}
于 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 に答える