在Java中创建二维数组的语法 [英] Syntax for creating a two-dimensional array in Java

查看:56
本文介绍了在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];

请注意,每个元素都将初始化为int0的默认值,因此上述内容也等同于:

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屋!

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