且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将一维字符串数组转换为二维字符数组

更新时间:2023-02-27 07:39:12

在内循环中使用 print 而不是 println 并且在每个循环之后打印一个带有 println.

Use print instead of println in inner loop and after each loop print a blank line with println.

for (int i = 0; i < 7; i++) // see changes 5/7. You did "new char[7][5]" not [5][7]
{
    for (int j = 0; j < 5; j++) // see changes 7/5
    {
         System.out.print(array2[i][j]);
    }
    System.out.println();
}

更新:

下面是一个将字符串数组转换为二维字符数组的程序.

Following is a program that convert String array to 2D char array.

public class StringToChar {
    public static void main(String[] args) {
        String[] strArr = { "HELLO", "WORLD" };
        char[][] char2D = new char[strArr.length][];

        for (int i = 0; i < strArr.length; i++) {
            char2D[i] = strArr[i].toCharArray();
        }

        for (char[] char1D : char2D) {
            for (char c : char1D)
                System.out.print(c + " ");

            System.out.println();
        }
    }
}