コンソール入力から取得した文字列を配列内の文字列と比較する場合、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]))
は常に戻ります。false
if (target.toString().equals(list[scan].toString())
ArrayList
リストの配列実装を表すために を使用しています。リストの先頭は、配列インデックス 0 に保持されます。このクラスは、特定の種類のリストを作成できるように拡張されています。必要に応じて、すべてのクラスを投稿できます。