In Java, arrays don't override toString()
, so if you try to print one directly, you get the className
+ '@' + the hex of the hashCode
of the array, as defined by Object.toString()
:
(在Java中,数组不会覆盖toString()
,因此,如果尝试直接打印一个,则将得到className
+'@'+数组的hashCode
的十六进制,如Object.toString()
所定义:)
int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // prints something like '[I@3343c8b3'
But usually, we'd actually want something more like [1, 2, 3, 4, 5]
.
(但是通常,我们实际上想要的是更多类似[1, 2, 3, 4, 5]
。)
What's the simplest way of doing that? (最简单的方法是什么?)
Here are some example inputs and outputs: (以下是一些示例输入和输出:)
// Array of primitives:
int[] intArray = new int[] {1, 2, 3, 4, 5};
//output: [1, 2, 3, 4, 5]
// Array of object references:
String[] strArray = new String[] {"John", "Mary", "Bob"};
//output: [John, Mary, Bob]
ask by Alex Spurling translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…