使用.equals()进行字符串比较在Java中不起作用。

使用.equals()进行字符串比较在Java中不起作用。,java,string-comparison,Java,String Comparison,将从控制台输入获取的字符串与数组中的字符串进行比较时,除非添加.toString(),否则该字符串始终为false。这两个字符串都是相等的,它应该在不添加.toString()的情况下工作。有人能帮我找出原因吗 在这里,我从控制台获取要比较的字符串: System.out.println("\nEnter the name you wish to remove from the list."); String name = in.nextLine(); System.out.println("\

将从控制台输入获取的字符串与数组中的字符串进行比较时,除非添加
.toString()
,否则该字符串始终为
false
。这两个字符串都是相等的,它应该在不添加
.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;
}
公共T删除(T元素){
T结果;
int index=find(元素);
如果(索引==未找到){
抛出新元素NotFoundException(“列表”);
}
结果=列表[索引];
后方--;
/**移动适当的元素*/
用于(int scan=索引;scan<后;scan++){
列表[扫描]=列表[扫描+1];
}
列表[后]=空;
返回结果;
}
以下是问题所在的查找方法:

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;
}
private int find(T目标){
int scan=0,结果=未找到;
布尔值=false;
如果(!isEmpty()){
同时(!找到并扫描<后){
if(target.equals(list[scan]){//在添加.toString()之前,我不确定这为什么不起作用
发现=真;
}
否则{
扫描++;
}
}
}
如果(找到){
结果=扫描;
}
返回结果;
}
if(target.equals(list[scan])
总是返回
false
,除非我将其更改为
if(target.toString().equals(list[scan].toString())


我正在使用
ArrayList
来表示列表的数组实现。列表的前面保留在数组索引0处。如果有帮助,可以扩展此类以创建特定类型的列表。如果需要,我可以发布所有类。

如果第一个参数是字符串,则仅使用String.equals

使用.equals()进行字符串比较在java中不起作用

看来这是起作用的东西,它的T.equals()不起作用


如果您有此功能,则表示您已明智地重写了
toString()

target.toString().equals(list[scan].toString()
但如果这不起作用

target.equals(list[scan])

这意味着您没有正确重写
equals(Object)

如果
myNameList
有一个
String
泛型参数,那么这将不起作用,因为没有
String
将等于
MyOrderedList
的类型


如果
myNameList
有一个
MyOrderedList
泛型参数,那么您需要确保为它定义一个
equals()
方法。

nicholas和peter是正确的。我需要重写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相当陌生,所以当我遇到问题时,我在这里读了很多帖子,我总是在这里找到答案。谢谢大家!

列表的定义在哪里??我想这是由于泛型-看看“target”变量-它真的是字符串的实例吗?尝试实现你自己的equals()和跟踪它…不要以为因为你不能使用它,它就坏了。如果字符串上的.equals()坏了,那将是众所周知的。你对我们做了不公正的事!!在我们想到答案之前你回答了所有的问题确实是最严重的不公正:)@Yadnesh好的,好的,我会让它休息一下“我会尽量等到问题提出10分钟后再回答。@Yadnesh我很难分辨区别。