EDIT2: 皆さんから多くの素晴らしい提案を受けた後、 を に変更することにしましD
たT
。課題で与えられたメソッドは を指定していましたD
が、T
動作し、あなたの圧倒的多数が であると考えているためT
、私はそれを変更し、インストラクターが対処できるようにします。すべてを修正して機能させる魔法のような回避策を見つけたらD
、ここに投稿します。助けてくれてありがとう!今日はたくさんのことを学びました。
編集:最初はこれについて明確ではなかったと思うので、繰り返したいと思います.単に変更することはできませんT next()
. 割り当ての説明では、メソッドを としてリストしますD next()
。私はそれを変えることはできません、それが私に与えられた方法です. 変更を提案してくださった皆様、お時間をいただきありがとうございますが、それはオプションではありません。
カスタム List クラスの「スケルトン」が提供された課題に取り組んでおり、すべてのメソッドを作成する必要があります。最後のNode
中にクラスも提供されましたList
。メソッドがIterator
であるクラスを書くことになっています。ただし、ジェネリックはクラスにのみ表示されるため、記述しようとしているクラスは、型に解決できないことを伝え続けます。Node クラス内にクラスを記述しようとしましたが、それが得られます。next()
D next() throws NoSuchElementException
D
Node
Iterator
D
The return type is incompatible with Iterator<T>.next()
代入によると、イテレータ クラスは型T
である必要がありますList
が、next()
メソッドは型 D を返さなければなりません。Iterator
クラスに型D
がNode
クラスと同じ?他のメソッドを実装せずにスケルトン コードを含めました。作成しようとしている 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() {
}
}
}