5

コンソール入力から取得した文字列を配列内の文字列と比較する場合、false追加しない限り常に.toString()です。両方の文字列は等しく、. を追加しなくても機能するはず.toString()です。誰でも理由を理解するのを手伝ってもらえますか?

ここで、コンソールから比較したい文字列を取得します。

System.out.println("\nEnter the name you wish to remove from the list.");
String name = in.nextLine();
System.out.println("\n\"" + myNameList.remove(new MyOrderedList(name)) + "\"" + " removed from the name list\n");

削除方法は次のとおりです。

public T remove(T element) {
    T result;
    int index = find(element);

    if (index == NOT_FOUND) {
        throw new ElementNotFoundException("list");
    }

    result = list[index];
    rear--;

    /** shift the appropriate elements */
    for (int scan = index; scan < rear; scan++) {
        list[scan] = list[scan+1];
    }

    list[rear] = null;
    return result;
}

問題があったfindメソッドは次のとおりです。

private int find(T target) {
    int scan = 0, result = NOT_FOUND;
    boolean found = false;

    if (!isEmpty()) {
        while (!found && scan < rear) {
            if (target.equals(list[scan])) { // Not sure why this does not work until I add the .toString()s
                found = true;
            }
            else {
                scan++;
            }
        }
    }

    if (found) {
        result = scan;
    }
    return result;
}

に変更しない限り、if (target.equals(list[scan]))は常に戻ります。falseif (target.toString().equals(list[scan].toString())

ArrayListリストの配列実装を表すために を使用しています。リストの先頭は、配列インデックス 0 に保持されます。このクラスは、特定の種類のリストを作成できるように拡張されています。必要に応じて、すべてのクラスを投稿できます。

4

3 に答える 3

3

最初の引数が文字列の場合、String.equals のみを使用しています。

.equals() を使用した文字列比較が機能しない Java

これは機能するもののようです。その T. equals() は機能しません。


これが機能している場合は、toString()賢明にオーバーライドしたことを意味します。

target.toString().equals(list[scan].toString()

しかし、これがうまくいかない場合

target.equals(list[scan])

equals(Object)正しくオーバーライドしていないことを意味します。

于 2012-09-17T13:29:54.343 に答える
1

myNameListにジェネリック パラメータがある場合String、これは機能しません。 noStringは の型に等しいからですMyOrderedList

myNameListジェネリック パラメータがある場合は、そのメソッドMyOrderedListを定義する必要がありますequals()

于 2012-09-17T13:30:20.077 に答える
0

ニコラスとピーターは正しいです。equals() メソッドをオーバーライドする必要がありました。私はいくつかのことを試し、Eclipse に hashCode() と equals() を生成させて、.toString() なしで何が起こるかを確認しました。

Eclipseが私のために生成したものは次のとおりです。

    /* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((myList == null) ? 0 : myList.hashCode());
    return result;
}

/* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof MyOrderedList)) {
        return false;
    }
    MyOrderedList other = (MyOrderedList) obj;
    if (myList == null) {
        if (other.myList != null) {
            return false;
        }
    } else if (!myList.equals(other.myList)) {
        return false;
    }
    return true;
}

このような迅速な対応に本当に感謝しています。私はJavaにかなり慣れていないので、問題が発生したときにここで多くの投稿を読み、常にここで答えを見つけます. みんな、ありがとう!

于 2012-09-17T16:41:21.113 に答える