在Java中,数组不会覆盖toString(),因此如果您尝试直接打印数组,则会获得className + @ + className数组的十六进制,如下所示hashCode:
int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // prints something like '[I@3343c8b3'
Run Code Online (Sandbox Code Playgroud)
但通常我们实际上想要更像的东西Object.toString().这样做最简单的方法是什么?以下是一些示例输入和输出:
// 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]
Run Code Online (Sandbox Code Playgroud) 请参阅相关的.NET问题
我正在寻找一种快速简便的方法来完成与分裂完全相反的方式,以便它 ["a","b","c"]能够成为"a,b,c"
迭代数组需要添加条件(如果这不是最后一个元素,添加分隔符)或使用子字符串删除最后一个分隔符.
我确信有一种经过认证的有效方法(Apache Commons?)
您更喜欢在项目中做到这一点?