如何在Java中初始化字符串的二维数组 [英] How to initialize a 2D array of strings in java

查看:77
本文介绍了如何在Java中初始化字符串的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何声明数组,并且已经声明了:

I know how to declare the array and I have done so:

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

现在我要使它成为默认情况下每个空格都是-(不带引号课程)。我看到了其他类似的问题,但答案对我来说没有意义。我所能理解的就是,我可能需要一个for循环。

Now I want to make it so that every space is "-" by default (without the quotes of course). I have seen other questions like this but the answers didn't make sense to me. All I can understand is that I would probably need a for loop.

推荐答案

我能想到的最快的方法是使用< a href = http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#fill%28java.lang.Object[],%20java.lang.Object%29 rel = noreferrer> Arrays.fill(Object [],Object) 像这样,

The quickest way I can think of is to use Arrays.fill(Object[], Object) like so,

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

这是使用 For-Each 迭代 String [] board1 ),并用-填充。然后,它使用 Arrays.deepToString(Object []) 进行打印。

This is using a For-Each to iterate the String[](s) of board1 and fill them with a "-". It then uses Arrays.deepToString(Object[]) to print it.

这篇关于如何在Java中初始化字符串的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆