为什么 toCharArray() 和 toString() 方法不起作用

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

Why toCharArray() and toString() method doesn't work

问题

public static void main(String[] args) {

    ArrayList<String> ar = new ArrayList<>(Arrays.asList("abc", "def", "ghi", "jkl"));

    char[] t = "abc".toCharArray();
    String t2 = Arrays.toString(t);

    if (ar.contains(t2)) {
        System.out.println("contains"); // This line doesn't get executed
    }

    String t3 = t.toString();
    if (ar.contains(t3)) {
        System.out.println("contains"); // This line doesn't get executed
    }

    if (ar.contains("abc")) {
        System.out.println("contains"); // This line gets executed and prints on console
    }
}
英文:

The toCharArray() in Java returns the Character Array and Arrays.toString() converts a array to string and toString() does same but why this below code doesn't work.

public static void main(String[] args) {

    ArrayList&lt;String&gt; ar = new ArrayList&lt;&gt;(Arrays.asList( &quot;abc&quot;, &quot;def&quot;, &quot;ghi&quot;, &quot;jkl&quot;));

    char[] t = &quot;abc&quot;.toCharArray();
    String t2 = Arrays.toString(t);

    if( ar.contains(t2)){
        System.out.println(&quot;contains&quot;); // This line doesn&#39;t get executed
    }

    String t3 = t.toString();
    if( ar.contains(t3)){
        System.out.println(&quot;contains&quot;); // This line doesn&#39;t get executed
    }

    if( ar.contains(&quot;abc&quot;)){
        System.out.println(&quot;contains&quot;); // This line execute and prints on console
    }
}

答案1

得分: 1

toString() 在字符数组上调用不会产生相同的结果。

当你写下:

String t3 = t.toString();

你实际上是想调用Object.class中的toString()方法。
因为字符数组无法拥有一个重写版本的toString()方法来完成你期望的操作,因为它是一个原始类型的数组。
它的工作原理如预期般,它会返回将特定字符数组链接在堆中的字符串表示。

Arrays.toString(t) 会返回初始数组的字符串表示。
在你的情况下,它会返回 "[a, b, c]"
然后,当你尝试调用 ar.contains(t2) 时,你实际上在初始数组中寻找字符串 "[a, b, c]",但该数组中并没有它。它内部有4个字符串。
"abc", "def", "ghi", "jkl"
它不包含所请求的元素("[a, b, c]"字符串)。

我建议你对程序进行调试。这样你可以看到实际上变量中包含的值。这将让你对这种情况有更多的理解。

英文:

toString() called on a char array doesn't do the same.

When you write

String t3 = t.toString();

you said that you want to call toString() method from Object.class.
Because char array cannot have an overrided version of toString() that will do what you re expected, because it's an array of primitive type.
It works as expected, it returns you the string representation of a link to that particular char array in a heap.

Arrays.toString(t) returns you the string representation of an initial array.
In your case it will return &quot;[a, b, c]&quot;.
Then when you're trying to call ar.contains(t2) you re trying to find &quot;[a, b, c]&quot; string in your initial array which doesn't have it. It have 4 strings inside.
&quot;abc&quot;, &quot;def&quot;, &quot;ghi&quot;, &quot;jkl&quot;.
It doesn't contain the requested element (&quot;[a, b, c]&quot; string).

I would recommend you to debug your program. You will see which values are actually in your variables. It will give you a lot more understanding about this situation.

答案2

得分: 0

Arrays.toString返回[a,b,c],与abc不相等。查看上面的回答,了解另一个为什么不起作用。

英文:

Arrays.toString returns [a,b,c] which is not equal to abc. See above answers why the other one didn't work.

答案3

得分: 0

你正在假设t.toString()会返回一个由数组t中所有字符组成的字符串。事实并非如此,语言也不要求如此。

你想要的是 String t3 = new String(t)

英文:

You are assuming that t.toString() will return a String that is made up of all characters in the array t. This is not the case, and nor does the language require it to be.

What you want is String t3 = new String(t)

答案4

得分: 0

代码按预期正常工作。实现此操作的理想方法是:

String t2 = String.valueOf(t);
if (ar.contains(t2)) {
    System.out.println("contains"); // 此行被执行,并在控制台上打印输出
}
英文:

The code works exactly as expected. The ideal way to do this would be

String t2 = String.valueOf(t);
if(ar.contains(t2)){
    System.out.println(&quot;contains&quot;); // This line execute and prints on console
}

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

发表评论

匿名网友

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

确定