콘솔 입력에서 가져온 문자열과 배열 내부의 문자열을 비교할 때 .toString()
을 추가하지 않으면 항상 false
이됩니다. 두 문자열은 동등하므로 .toString()
을 추가하지 않으면 작동합니다. 아무도 내가 왜 알아낼 수 있습니까? 여기.equals()를 사용한 문자열 비교는 Java에서 작동하지 않습니다.
내가 콘솔에서 비교하려는 문자열을 얻을 : 여기
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;
}
문제였다됩니다 찾기 방법입니다 : 여기
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");
은 제거 방법이다
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에 보관 유지됩니다.이 클래스는, 그것이 도움이되는 경우, 특정의 종류의리스트를 작성하도록 확장됩니다. 필요한 경우 모든 수업을 게시 할 수 있습니다.
여기서'list'의 정의는 무엇입니까? –
제네릭 때문인 것 같아요. 'target'변수를 봐주세요. 정말로 String의 인스턴스입니까? 자신의 equals()를 구현하고 추적 해보십시오. – shmoula
사용할 수 없기 때문에 깨졌습니다. String의 .equals()가 깨진 경우 꽤 잘 알려져 있습니다. –