如何比较两个字符?

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

How can I compare two chars?

问题

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

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

我也尝试过这个:

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

但它也不起作用。

英文:

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.

  1. String out = "";
  2. for(int i = 0; i < strArr[0].length(); i++){
  3. char x = strArr[0].charAt(i);
  4. for(int j = 0; j < strArr[1].length(); j++){
  5. char y = strArr[1].charAt(j);
  6. if(x.equals(y)){
  7. out.concat(x + ",");
  8. }
  9. }
  10. }
  11. if(out.equals("")){
  12. out = "false";
  13. }

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

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

I've also tried this:

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

But it doesn't work either.

答案1

得分: 2

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

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

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

  1. StringBuilder out = new StringBuilder();
  2. for(int i = 0; i < strArr[0].length(); i++){
  3. char x = strArr[0].charAt(i);
  4. for(int j = 0; j < strArr[1].length(); j++){
  5. char y = strArr[1].charAt(j);
  6. if (x == y){
  7. out.append(x + ",");
  8. }
  9. }
  10. }
英文:

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.

  1. StringBuilder out = new StringBuilder();
  2. for(int i = 0; i &lt; strArr[0].length(); i++){
  3. char x = strArr[0].charAt(i);
  4. for(int j = 0; j &lt; strArr[1].length(); j++){
  5. char y = strArr[1].charAt(j);
  6. if (x == y){
  7. out.append(x + &quot;,&quot;);
  8. }
  9. }
  10. }

答案2

得分: 0

比较原始类型使用 ==

字符串是不可变的。

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

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

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.

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

答案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.

  1. String out = &quot;&quot;;
  2. HashSet&lt;String&gt; hashSet = new HashSet&lt;&gt;();
  3. hashSet.addAll(strArr[0]);
  4. hashSet.retainAll(strArr[1]);
  5. out = hashSet.toString();
  6. if(out.equals(&quot;&quot;)){
  7. out = &quot;false&quot;;
  8. }

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:

确定