英文:
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<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 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 "[a, b, c]"
.
Then when you're trying to call ar.contains(t2)
you re trying to find "[a, b, c]"
string in your initial array which doesn't have it. It have 4 strings inside.
"abc"
, "def"
, "ghi"
, "jkl"
.
It doesn't contain the requested element ("[a, b, c]" 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("contains"); // This line execute and prints on console
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论