简体   繁体   中英

How do I add String to a char array?

public static void main (String[] args) {
    char[][] c = {{'a', 'b', 'c'},
                  {'d', 'e', 'f'}};
    show(c);        
}
public static void show (char[][] c) {
    for (int i = 0; i < c.length; i++) {
        System.out.println(c[i]);

I want a space between each letter. I tried to write + " " after c[i] but then I get this warning: "Must explicitly convert the char[] to a String". How I am supposed to add a string to my array? Thanks in advance!

Right now what you are doing wrong is, you are printing each sub-array. I'm not sure if I understood you correctly. But if you want to print each char of your 2D char array with space between each letter, then you should use two for loops to iterate over the whole 2D array and print each char like this:

public static void main(String[] args) {
    char[][] c = { { 'a', 'b', 'c' }, { 'd', 'e', 'f' } };
    show(c);
}

public static void show(char[][] c) {
    for (int i = 0; i < c.length; i++) {
        for (int j = 0; j < c[i].length; j++) {
            System.out.print(c[i][j] + " ");
        }
    }
}

Output:

a b c d e f 

Edit:

To print each sub-array in a seperate line, simply change the show method like this:

public static void show(char[][] c) {
    for (int i = 0; i < c.length; i++) {
        for (int j = 0; j < c[i].length; j++) {
            System.out.print(c[i][j] + " ");
        }
        System.out.println(); // add a println here
    }
}

New output:

a b c 
d e f 

With Java Streams you can do it:

    Arrays.stream(c).map(String::valueOf)
                    .map(i -> i.replace("", " "))
                    .forEach(System.out::print);

for the output

a b c  d e f 

or:

   Arrays.stream(c).map(String::valueOf)
                   .map(i -> i.replace("", " "))
                   .forEach(System.out::println);

for the output:

a b c 
d e f 

For each array of chars in the 2D array of chars we convert to a String :

map(String::valueOf)

then we add a " " between the character of each String:

 map(i -> i.replace("", " "))

finally we print the result each String:

forEach(System.out::println)

You can use String.codePoints method to iterate over int values of the characters of this 2d array and add spaces between them:

public static void main(String[] args) {
    char[][] chars = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
    System.out.println(spaceBetween(chars)); // a b c d e f
}
public static String spaceBetween(char[][] chars) {
    return Arrays.stream(chars)
            // Stream<char[]> to IntStream
            .flatMapToInt(arr -> String.valueOf(arr).codePoints())
            // codepoints as characters
            // return Stream<Character>
            .mapToObj(ch -> (char) ch)
            // characters as strings
            // return Stream<String>
            .map(String::valueOf)
            // join characters with
            // whitespace delimiters
            .collect(Collectors.joining(" "));
}

See also: Is there any other way to remove all whitespaces in a string?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM