英文:
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 < 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 + ",");
        }
    }
}
答案2
得分: 0
比较原始类型使用 ==。
字符串是不可变的。
调用 out.concat("whatever") 不会改变 out 的值。你必须将返回的值赋给 out。
if (x == y) { // 使用 == 比较原始类型
    out = out.concat(x + ","); // 赋值返回的值
}
英文:
Compare primitive types with ==.
Strings are immutable.
Calling out.concat("whatever")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 + ","); // 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<T> 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<T> 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";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论