2D arrays of chars can be easily converted into single string using String(char[])
constructor for each "row" array of chars followed by joining the resulting strings:
static String singleString(char[][] arr) {
return Arrays.stream(arr)
.map(String::new)
.collect(Collectors.joining(""));
}
"Streamless" version using StringBuilder
may look as follows:
static String singleString(char[][] arr) {
StringBuilder sb = new StringBuilder(arr.length * (arr.length > 0 ? arr[0].length : 0));
for (char[] row : arr) {
sb.append(new String(row));
}
return sb.toString();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…