将二维数组转换为字符串 [英] convert two-dimensional array to string
问题描述
我正在尝试将 String [] []
转换为字符串以在GUI中显示。这是我的代码:
I'm trying to convert a String[][]
to a string for display in a GUI. Here's my code:
String[][] tableBornes = SearchPropertiesPlugin.FillBornesTable();
String table = tableBornes.toString();
以下是它的产生:
[[Ljava.lang.String; @ 19b8858
如何获得更具可读性的内容?
How can I get something more readable?
推荐答案
如果要创建数组的单行表示,可以使用 Arrays.deepToString
。
If you want to create one-line representation of array you can use Arrays.deepToString
.
如果您想创建多个行代表你可能需要遍历所有行并附加结果
Array.toString(array [row])
like
In case you want to create multi-line representation you will probably need to iterate over all rows and append result of Array.toString(array[row])
like
String[][] array = { { "a", "b" }, { "c" } };
String lineSeparator = System.lineSeparator();
StringBuilder sb = new StringBuilder();
for (String[] row : array) {
sb.append(Arrays.toString(row))
.append(lineSeparator);
}
String result = sb.toString();
从Java 8开始,你甚至可以使用 StringJoiner
with会自动为你添加分隔符:
Since Java 8 you can even use StringJoiner
with will automatically add delimiter for you:
StringJoiner sj = new StringJoiner(System.lineSeparator());
for (String[] row : array) {
sj.add(Arrays.toString(row));
}
String result = sj.toString();
或使用流
String result = Arrays
.stream(array)
.map(Arrays::toString)
.collect(Collectors.joining(System.lineSeparator()));
这篇关于将二维数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!