挿入、検索、および削除機能を備えたリンク リストを作成しました。そのためのイテレータも作成しました。さて、私がこれをするとします:
myList<Integer> test = new myList();
test.insert(30);
test.insert(20);
test.insert(10);
myList.iterator it = test.search(20);
if(it.hasNext())
System.out.println(it.next());
そしてほら、それは機能します(ノードの要素の値、この場合は20を出力します)。今、私がこれを行うと:
myList<Double> test = new myList();
test.insert(30.1);
test.insert(20.1);
test.insert(10.1);
myList.iterator it = test.search(20.1);
if(it.hasNext())
System.out.println(it.next());
イテレータが null を指しているため、そうではありません。検索機能の実装は次のとおりです。
public iterator search(T data)
{
no<T> temp = first;
while( (temp != null) && (temp.data != data) )
temp = temp.next;
return (new iterator(temp));
}
上記のコードの一部を次のように変更すると、次のようになります。
while( (temp != null) && (temp.data != data) )
System.out.println(temp.data + " " + data);
temp = temp.next;
リストに数字が出力されているのがわかります。ある時点で「20.1 20.1」と出力されます (たとえば)。では、どうすればこれを修正できますか?関数は正しいように見えますが、Java が数値を正しく比較していないように見えます。
EDIT:ところで、BigDecimalも同じ種類の問題を私に与えました。
EDIT 2: equals() は機能しましたが、他に何か問題があることに気づきませんでした。ごめん。