本文介绍了如果使用==的语句给出了意外的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void refineWords() {
    for(String word : words){
        Log.i("word", word);
        if (word == "s" || word == "t" || word == "am" || word == "is" || word == "are" || word == "was" || word == "were" || word == "has" ||
            word == "have" || word == "been" || word == "will" || word == "be" || word == "would" || word == "should" || word == "shall" ||
            word == "must" || word == "can" || word == "could" || word == "the" || word == "as" || word == "it" || word == "they" ||
            word == "their" || word == "he" || word == "she" || word == "his" || word == "her" || word == "him" || word == "its" ||
            word == "in" || word == "on" || word == "a" || word == "at") {

            Log.i("step", "step Success!!");
            words.remove(word);
        }
    }
}

我有一个名为单词,它包含字符串。这里Log.i适用于word标签,但step语句不会执行。似乎If条件不能正常工作。虽然单词列表包含类似的字符串,但是这种方法永远不会进入它。会出现什么问题。请求帮助..

I have a List called "words" and it contains strings. Here the Log.i works for the "word" tag fine but the "step" Statement does not executes. Seems the If condition does not work well. like this method never goes into it although the "words" list contains similar strings. What would be the problem. pleas help..

推荐答案

您需要使用 String.equals(),而不是 == == 检查两个 Object 引用是否引用相同的对象

You need to use String.equals(), not ==. == checks if two Object references refer to the same Object:

if("s".equals(word) || "t".equals(word) || ...

来自 15.21.3部分参考等式运算符==和!= Java语言规范3.0

这篇关于如果使用==的语句给出了意外的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 02:25
查看更多