2

EDIT2: 皆さんから多くの素晴らしい提案を受けた後、 を に変更することにしましDT。課題で与えられたメソッドは を指定していましたDが、T動作し、あなたの圧倒的多数が であると考えているためT、私はそれを変更し、インストラクターが対処できるようにします。すべてを修正して機能させる魔法のような回避策を見つけたらD、ここに投稿します。助けてくれてありがとう!今日はたくさんのことを学びました。


編集:最初はこれについて明確ではなかったと思うので、繰り返したいと思います.単に変更することはできませんT next(). 割り当ての説明では、メソッドを としてリストしますD next()。私はそれを変えることはできません、それが私に与えられた方法です. 変更を提案してくださった皆様、お時間をいただきありがとうございますが、それはオプションではありません。


カスタム List クラスの「スケルトン」が提供された課題に取り組んでおり、すべてのメソッドを作成する必要があります。最後のNode中にクラスも提供されましたList。メソッドがIteratorであるクラスを書くことになっています。ただし、ジェネリックはクラスにのみ表示されるため、記述しようとしているクラスは、型に解決できないことを伝え続けます。Node クラス内にクラスを記述しようとしましたが、それが得られます。next()D next() throws NoSuchElementExceptionDNodeIteratorDThe return type is incompatible with Iterator<T>.next()

代入によると、イテレータ クラスは型Tである必要がありますListが、next()メソッドは型 D を返さなければなりません。Iteratorクラスに型DNodeクラスと同じ?他のメソッドを実装せずにスケルトン コードを含めました。作成しようとしている Iterator クラスは、「追加クラス」の下の一番下にあります。

どうすればこのnext()メソッドを機能させることができますか? これを私に説明するために時間を割いてくれた人に感謝します。

package list;

import java.lang.Iterable;
import java.util.Iterator;
import java.lang.IndexOutOfBoundsException;
import java.util.NoSuchElementException;

public class List<T extends Comparable<T>> implements Iterable<T> {

    public class Node<D extends Comparable<D>> {
        private D data;
        private Node<D> next;
    }

    private Node head;

    public List() {
        head = null;
    }

    public boolean add(T newElt) {
        if(head == null){
            head.data = newElt;
            head.next = null;
            return true;
        }
        return false;
        //Unfinished.  Iterate to check for duplicates.
    }

    public T getFirst() throws NoSuchElementException {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public T get(int index) throws IndexOutOfBoundsException {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public T lookup(T element) {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public int size() {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public void delete(T element) throws NoSuchElementException {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public void reset() {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public String toString() {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public List<T> subList(int start, int end) throws NoSuchElementException {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public void removeSubList(int start, int end) throws NoSuchElementException {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public boolean equals(Object obj) {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public Iterator<T> iterator() {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public Comparator<List<T>> lengthComparator() {
        throw new UnsupportedOperationException("You must write this method.");
    }

    public Comparator<List<T>> orderComparator() {
        throw new UnsupportedOperationException("You must write this method.");
    }


    /*
     * Additional classes
     */


    public class ListIterator<T> implements Iterator<T>{

        Node current = head; 

        public boolean hasNext() {
            if(head.next == null){
                return false;
            }
            return true;
        }

        public D next() throws NoSuchElementException{
            return null;
        }

        public void remove() {

        }

    }

}
4

4 に答える 4

1

あなたが持っている場所:

private Node head;

代わりに次のものが必要です。

private Node<T> head;

同じことがNode current = head;イテレータにも当てはまります。

定義したクラスはジェネリックであることを忘れないでください。生の代わりにNode常に使用する必要があるのと同じように、常にそのジェネリックパラメーターを指定する必要があります。以来、コンパイラはそれが要件を満たしていることを認識します。List<Foo>ListT extends Comparable<T>NodeD extends Comparable<D>


無関係:の代わりに を作成Nodeすることを検討する必要があります。のユーザーは内部リンク リストの実装を気にする必要がないため、カプセル化に適しています。内部クラスが実際にその外側のクラスのフィールドにアクセスする必要がない限り (つまり、あなたのコードにはない のフィールドにアクセスする必要がない限り)、 は良い習慣です。private static classpublic classprivateListstaticNodeList

于 2013-10-15T20:36:39.887 に答える
0

イテレータ クラスは次のようになります。

public class ListIterator<T> implements Iterator<T>{

    Node<T> current = head; 

    public boolean hasNext() {
        if(current.next == null){ // check if next node exists
            return false; // doesn't exist
        }
        return true; // exists
    }

    public D next() throws NoSuchElementException{
        if (current.next == null){ // check if there is next node
            throw new NoSuchElementException(); // if not throw new exception
        }
        current = current.next; // changing to next node
        return current.data; // provide the data in next node
    }

    public void remove() {
        // since Node class save the reference to next Node and doesn't for previous, 
        // removing objects it quite difficult, because it will break the lis
    }

    }
于 2013-10-15T20:32:40.467 に答える
0

次のようなコードでノードを作成するため、NodeクラスにはDa と同じものが含まれます。T

Node<T> newNode = new Node<T>();
newNode.data = newElt.

ここでDTが等しくなります。

于 2013-10-15T20:32:50.327 に答える