在 Java 中创建二维数组的语法 [英] Syntax for creating a two-dimensional array in Java
本文介绍了在 Java 中创建二维数组的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑:
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 }
};
这篇关于在 Java 中创建二维数组的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文