如何打印2d数组的结果

时间:2015-12-11 04:08:46

标签: java arrays string matrix 2d

the out come should look like this

String word = "grumpy";
int size = word.length();
char[][] matrix = new char[size][size];
for(int i=0; i<size; i++ )
{
   matrix[0][i]=word.charAt(i);
   matrix[size-1][i]=word.charAt(size-1-i);
   matrix[i][size-1]=word.charAt(size-1-i);
   matrix[i][0]=word.charAt(i);
}

请帮忙。 我遇到问题如何打印我的代码结果应该看起来像this

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情

   for (int i = 0; i < matrix.length; i++) {
        System.out.println(Arrays.toString(matrix[i]).replaceAll(",","")
                                                   .replaceAll("\u0000"," "));
    }

这会让你出局,

[g r u m p y]
[r         p]
[u         m]
[m         u]
[p         r]
[y p m u r g]

答案 1 :(得分:1)

for(int i = 0; i < size; i++) }
    for(int j = 0; j < size; j++) {
        if(i == 0 || i == size - 1 || j == 0 || j == size - 1) {
            System.out.print(matrix[i][j] + ' ');
        } else {
            System.out.print('  ');
        }
    }
    System.out.println();
}