用于创建二维数组的语法 [英] Syntax for creating a two-dimensional array
本文介绍了用于创建二维数组的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑:
int[][] multD = new int[5][];
multD[0] = new int[10];
这是如何创建一个包含5行和10列的二维数组?
Is this how you create a two-dimensional array with 5 rows and 10 columns?
我在网上看到了这段代码,但语法没有意义。
I saw this code online, but the syntax didn't make sense.
推荐答案
尝试以下方法:
int[][] multi = new int[5][10];
...这是这样的简写:
... which is a short hand for something like this:
int[][] multi = new int[5][];
multi[0] = new int[10];
multi[1] = new int[10];
multi[2] = new int[10];
multi[3] = new int[10];
multi[4] = new int[10];
请注意,每个元素都将初始化为 int的默认值
, 0
,所以上述内容也相当于:
Note that every element will be initialized to the default value for int
, 0
, so the above are also equivalent to:
int[][] multi = new int[][]{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
这篇关于用于创建二维数组的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文