1

My goal is to print a new string like

char[] result = someArray();
System.out.print(new string(result))

I am working with 2 dimentional array in my task char[][].

I would like to convert my char[][] array to char[]

How do I either print a single string directly from char[][] or easily convert it to char[]?

Arrays.toDeepString() does not work for me as it prints in the format [[a,b,c],[d,e,f]] and I am looking for an output that is abcdef.

5 Answers5

2

You can do the following:

  • the stream streams the 1D arrays.
  • the String::valueOf maps each of those arrays to a String
  • the joining joins the string with an empty delimiter.
char[][] chs = {{'a','b'},{'c','d','e'}};
String str = Arrays.stream(chs)
      .map(String::valueOf)
      .collect(Collectors.joining());

System.out.println(str);

Prints

abcde

If you want to print each array separately, you can do the following:

for (char[] ch : chs) {
   System.out.println(Arrays.toString(ch));
}

Prints

[a, b]
[c, d, e]
WJS
  • 36,363
  • 4
  • 24
  • 39
2

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();
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
1

Simply iterate all array and concat rows / columns according to the logic you want to implement.

Say you have the following:

[[a] [a] [a]]
[[b] [b] [b]]
[[c] [c] [c]]

iterate the first row, and create a new char array with "aaa", second one - "bbb", third one - "ccc".

[aaa][bbb][ccc]
javadev
  • 688
  • 2
  • 6
  • 17
0

Alternatively:

char[][] matrix_char = {{'a', 'b'}, {'c', 'd', 'e'}};
String result = Arrays.deepToString(matrix_char).replaceAll("[\\[ ,\\]]", "");
System.out.println(result);

Or the straightforward:

char[][] matrix_char = {{'a', 'b'}, {'c', 'd', 'e'}};
StringBuilder result = new StringBuilder();
for (char[] row : matrix_char)
    for (char col : row)
        result.append(col);
System.out.println(result.toString());

Output:

abcde
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
0

You can concatenate all of these characters into a single string and then convert this string to a 1d character array. This method works with character code points, so it can also handle unprintable characters:

char[][] arr2d = {{'a', 'b', 'c'}, {'d'}, {'e', 10, 0}};

char[] arr = Arrays.stream(arr2d)
        // concatenate each subarray into one string
        .map(String::new)
        // join all strings into a single string
        .collect(Collectors.joining())
        // convert this string to a character array
        .toCharArray();

// output
for (char ch : arr) {
    // integer codepoint and unicode name of the character
    System.out.println(0 + ch + " - " + Character.getName(ch));
}

Output:

97 - LATIN SMALL LETTER A
98 - LATIN SMALL LETTER B
99 - LATIN SMALL LETTER C
100 - LATIN SMALL LETTER D
101 - LATIN SMALL LETTER E
10 - LINE FEED (LF)
0 - NULL

See also: Why do I get a number when I add chars?