Java:将集合转换为字符串表示的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/992807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Converting a set to an array for String representation
提问by inglesp
From Sun's Java Tutorial, I would have thought this code would convert a set into an array.
从 Sun 的Java 教程中,我认为这段代码会将集合转换为数组。
import java.util.*;
public class Blagh {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("a");
set.add("b");
set.add("c");
String[] array = set.toArray(new String[0]);
System.out.println(set);
System.out.println(array);
}
}
However, this gives
然而,这给
[a, c, b]
[Ljava.lang.String;@9b49e6
What have I misunderstood?
我误解了什么?
采纳答案by coobird
The code works fine.
该代码工作正常。
Replace:
代替:
System.out.println(array);
With:
和:
System.out.println(Arrays.toString(array));
Output:
输出:
[b, c, a] [b, c, a]
The String
representation of an array displays the a "textual representation" of the array, obtained by Object.toString
-- which is the class name and the hash code of the array as a hexidecimal string.
String
数组的表示显示数组的“文本表示”,通过Object.toString
-- 它是数组的类名和哈希码作为十六进制字符串。
回答by jpalecek
I don't think you have misunderstood anything; the code should work. The array, however, is not smart enough to print its contents in the toString method, so you'll have to print the contents with
我不认为你有什么误解;代码应该可以工作。但是,该数组不够智能,无法在 toString 方法中打印其内容,因此您必须使用
for(String s : array) println(s);
or something like that.
或类似的东西。
回答by Reginaldo
It's OK.
没关系。
You are not seeing the array contents with System.out.println(array) because println calls object.toString() to get the bytes from an Object for output.
您没有看到 System.out.println(array) 的数组内容,因为 println 调用 object.toString() 从 Object 获取字节以进行输出。
Since HashSet overrides the default toString() implementation, you can see the set contents with System.out.println(set);
由于 HashSet 覆盖了默认的 toString() 实现,因此您可以使用 System.out.println(set); 查看设置内容。
As arrays do not override the default toString() (that gives the class name and some sort of identity hash code), you are getting the fuzzy [Ljava.lang.String;@9b49e6
由于数组没有覆盖默认的 toString()(给出类名和某种身份哈希码),你得到了模糊 [Ljava.lang.String;@9b49e6
Hope that helps
希望有帮助
回答by dfa
for the sake of completeness check also java.util.Arrays.toStringand java.util.Arrays.deepToString.
为了完整性检查,还有java.util.Arrays.toString和java.util.Arrays.deepToString。
The latter is particularly useful when dealing with nested arrays (like Object[][]).
后者在处理嵌套数组(如 Object[][])时特别有用。
回答by David
As dfa mentioned, you can just replace:
正如 dfa 提到的,您可以替换:
System.out.println(array);
with...
和...
System.out.println(Arrays.toString(array));
回答by Thorbj?rn Ravn Andersen
You have the correct result. Unfortunately the toString()-method on the array is still the original Object.toString() so the output is somewhat unusable per default but that goes for all arrays.
你有正确的结果。不幸的是,数组上的 toString() 方法仍然是原始的 Object.toString() 因此输出在默认情况下有些不可用,但这适用于所有数组。