在Java中初始化多维数组
声明多维数组并为其赋值的正确方法是什么?
这就是我所拥有的:
int x = 5;
int y = 5;
String[][] myStringArray = new String [x][y];
myStringArray[0][x] = "a string";
myStringArray[0][y] = "another string";
What is the correct way to declare a multidimensional array and assign values to it?
This is what I have:
int x = 5;
int y = 5;
String[][] myStringArray = new String [x][y];
myStringArray[0][x] = "a string";
myStringArray[0][y] = "another string";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Java 没有“真正的”多维数组。
例如,
arr[i][j][k]
相当于((arr[i])[j])[k]
。换句话说,arr
只是一个数组、数组、数组。因此,如果您知道数组如何工作,您就知道多维数组如何工作!
声明:
或者,通过初始化:
访问:
或
字符串表示:
产生
Java doesn't have "true" multidimensional arrays.
For example,
arr[i][j][k]
is equivalent to((arr[i])[j])[k]
. In other words,arr
is simply an array, of arrays, of arrays.So, if you know how arrays work, you know how multidimensional arrays work!
Declaration:
or, with initialization:
Access:
or
String representation:
yields
尝试将相应的行替换为:
您的代码不正确,因为子数组的长度为
y
,索引从 0 开始。因此设置为myStringArray[0][y] 或
myStringArray[0][x]
将失败,因为索引x
和y
超出范围。String[][] myStringArray = new String [x][y];
是初始化矩形多维数组的正确方法。如果您希望它是锯齿状的(每个子数组可能具有不同的长度),那么您可以使用类似于 这个答案。但请注意,如果您想要完美的矩形多维数组,约翰关于您必须手动创建子数组的断言是不正确的。Try replacing the appropriate lines with:
Your code is incorrect because the sub-arrays have a length of
y
, and indexing starts at 0. So setting tomyStringArray[0][y]
ormyStringArray[0][x]
will fail because the indicesx
andy
are out of bounds.String[][] myStringArray = new String [x][y];
is the correct way to initialise a rectangular multidimensional array. If you want it to be jagged (each sub-array potentially has a different length) then you can use code similar to this answer. Note however that John's assertion that you have to create the sub-arrays manually is incorrect in the case where you want a perfectly rectangular multidimensional array.您还可以使用以下构造:
You can also use the following construct:
您可以声明多维数组,例如:
You can declare multi dimensional arrays like :
Java 中的多维数组
返回多维数组
Java 并不真正支持多维数组。在Java中,二维数组只是数组的数组,三维数组是数组的数组的数组,四维数组是数组的数组的数组的数组,等等
......可以将二维数组定义为:
int[ ] num[ ] = {{1,2}, {1,2}, {1,2}, {1,2}}
int[ ][ ] num = new int[4][2]
如果不分配,比如说
num[2][1]
,它没有初始化,然后就自动分配0,即自动num[2] [1] = 0
;下面,
num1.length
为您提供行。而
num1[0].length
则为您提供与num1[0]
相关的元素数量。这里num1[0]
仅具有相关数组num1[0][0]
和num[0][1]
。这里我们使用了
for
循环来帮助我们计算num1[i].length
。这里i
通过循环递增。Multidimensional Array in Java
Returning a multidimensional array
Java does not truely support multidimensional arrays. In Java, a two-dimensional array is simply an array of arrays, a three-dimensional array is an array of arrays of arrays, a four-dimensional array is an array of arrays of arrays of arrays, and so on...
We can define a two-dimensional array as:
int[ ] num[ ] = {{1,2}, {1,2}, {1,2}, {1,2}}
int[ ][ ] num = new int[4][2]
If you don't allocate, let's say
num[2][1]
, it is not initialized and then it is automatically allocated 0, that is, automaticallynum[2][1] = 0
;Below,
num1.length
gives you rows.While
num1[0].length
gives you the number of elements related tonum1[0]
. Herenum1[0]
has related arraysnum1[0][0]
andnum[0][1]
only.Here we used a
for
loop which helps us to calculatenum1[i].length
. Herei
is incremented through a loop.我要补充的是,如果您想阅读尺寸,可以这样做:
您还可以有 锯齿状数组,其中不同的行有不同的长度,因此
a[0].length != a[1].length
。I'll add that if you want to read the dimensions, you can do this:
You can also have jagged arrays, where different rows have different lengths, so
a[0].length != a[1].length
.输出
1
2
3
4
5
6
7
5
6
7
8
9
10
11
Output
1
2
3
4
5
6
7
5
6
7
8
9
10
11