How to initialize 2D string array in Java

I know how to declare arrays. I've done this:

String[][] board1 = new String[10][10];

Now I want to do this. By default, every space is "–" (without quotation marks, of course) I've seen other questions like this, but the answer doesn't make sense to me What I can understand is that I may need a for loop

Solution

The fastest way I can think of is to use arrays like this fill(Object[],Object),

String[][] board1 = new String[10][10];
for (String[] row : board1) {
    Arrays.fill(row,"-");
}
System.out.println(Arrays.deepToString(board1));

This is to iterate over the string [] (s) of board1 using for each and fill them with "–" Then use arrays Deeptostring (object []) to print

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>