Java基础之二维数组的创建与访问

图片中给出的四种二维数组的创建方式都可以成功创建,但是方式2不能在未指定值的情况下直接访问,必须指定一维数组的大小才能访问相应位置,

由此我们可以得到结论:二维数组命名时变量名和[ ]可以随意排列。

同时需要注意必须指定维数大小,以下两种创建二维数组的方式是错误的:

float  f1[ ][ ] = new  float[ ][ ];——variable must provide either dimension expression or an array initializer(变量必须提供维度表达式或数组设定初始值)

float  f2[ ][ ] = new float[ ][ 6];——cannot  specify an array dimension after an empty dimension(不能在空维度后面指定数组纬度)

由此可以推断:二维数组创建必须指定一维数组的数量

猜你喜欢

转载自www.cnblogs.com/CKhomepage/p/10613158.html