How do I add String to a char array?

别说谁变了你拦得住时间么 提交于 2021-01-07 00:56:17

问题


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!


回答1:


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 



回答2:


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)



回答3:


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?



来源:https://stackoverflow.com/questions/65181889/how-do-i-add-string-to-a-char-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!