如何比较两个字符?

huangapple go评论57阅读模式
英文:

How can I compare two chars?

问题

问题:当运行代码时,它给出以下错误:

char cannot be dereferenced
if(x.equals(y)){
    ^

我也尝试过这个:

if(x == y){
    out.concat(x + ",");
}

但它也不起作用。

英文:

Situation: I have two strings stored in a string array.

Goal: I want to add all the chars which are in both strings to a string variable out.

String out = "";
for(int i = 0; i < strArr[0].length(); i++){
    char x = strArr[0].charAt(i);
    for(int j = 0; j < strArr[1].length(); j++){
        char y = strArr[1].charAt(j);
        if(x.equals(y)){
            out.concat(x + ",");
        }
    }
}

if(out.equals("")){
    out = "false";
}

Problem: When I run the code it gives me the following error:

char cannot be dereferenced
        if(x.equals(y)){
            ^

I've also tried this:

if(x == y){
    out.concat(x + ",");
}

But it doesn't work either.

答案1

得分: 2

要比较两个char值,请使用==运算符x == y

之所以似乎不起作用的原因是:您正在使用的out.concat()方法不会修改out。通常情况下,String是“不可变的” - 您无法更改String的值。

对于out变量,使用StringBuilder而不是String。

StringBuilder out = new StringBuilder();
for(int i = 0; i < strArr[0].length(); i++){
    char x = strArr[0].charAt(i);
    for(int j = 0; j < strArr[1].length(); j++){
        char y = strArr[1].charAt(j);
        if (x == y){
            out.append(x + ",");
        }
    }
}
英文:

To compare two char values, use the == operator x == y.

The reason why it didn't seem to work is another: the out.concat() method you are using does not modify out. In general, String is "immutable" - you cannot change the value of a String.

Use StringBuilder instead of String for the out variable.

StringBuilder out = new StringBuilder();
for(int i = 0; i &lt; strArr[0].length(); i++){
    char x = strArr[0].charAt(i);
    for(int j = 0; j &lt; strArr[1].length(); j++){
        char y = strArr[1].charAt(j);
        if (x == y){
            out.append(x + &quot;,&quot;);
        }
    }
}

答案2

得分: 0

比较原始类型使用 ==

字符串是不可变的。

调用 out.concat("whatever") 不会改变 out 的值。你必须将返回的值赋给 out

if (x == y) { // 使用 == 比较原始类型
    out = out.concat(x + ","); // 赋值返回的值
}
英文:

Compare primitive types with ==.

Strings are immutable.

Calling out.concat(&quot;whatever&quot;)doesn't change the value of out. You have to assign the returned value to out.

if(x == y){ // compare primitive with ==
    out = out.concat(x + &quot;,&quot;); // assign the returned value
}

答案3

得分: 0

以下是翻译好的部分:

"Others have mentioned the problem you are having with String being immutable, I would go a step further and suggest an alternate approach over iterating through your strings. HashSet&lt;T&gt; has two methods that should simplify your code. addAll and retainAll.

String out = "";

HashSet<String> hashSet = new HashSet<>();
hashSet.addAll(strArr[0]);
hashSet.retainAll(strArr[1]);

out = hashSet.toString();

if(out.equals("")){
out = "false";
}"

英文:

Others have mentioned the problem you are having with String being immutable, I would go a step further and suggest an alternate approach over iterating through your strings. HashSet&lt;T&gt; has two methods that should simplify your code. addAll and retainAll.

String out = &quot;&quot;;

HashSet&lt;String&gt; hashSet = new HashSet&lt;&gt;();
hashSet.addAll(strArr[0]);
hashSet.retainAll(strArr[1]);

out = hashSet.toString();

if(out.equals(&quot;&quot;)){
    out = &quot;false&quot;;
}

huangapple
  • 本文由 发表于 2020年8月10日 22:32:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63342254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定