[英]Java Two Dimensional Array Definition
I have quite a basic question. 我有一个基本的问题。
For example: 例如:
int a[][] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}
we say it is [3][4] as it has 3 elements and each element has its own 4 elements. 我们说它是[3] [4],因为它有3个元素,每个元素都有自己的4个元素。
but how about: 但怎么样:
int a[][] = {{1},{5,6,7},{9,10,11,12}}
is it still [3][4]? 还在[3] [4]吗?
update 更新
so based on the helpful comments below, my first example can be written as a [3][4] array, but the second one cannot be indicated like that, right? 所以基于下面的有用评论,我的第一个例子可以写成[3] [4]数组,但第二个例子不能像那样表示,对吧?
These are called Jagged Arrays , and they are not considered to be m-by-n , because they do not fit the formal definition of a Matrix . 这些被称为Jagged Arrays ,它们不被认为是m-by-n ,因为它们不符合Matrix的形式定义。
In mathematics, a matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns.
在数学中,矩阵(多个矩阵)是以行和列排列的数字,符号或表达式的矩形阵列。
According to java 根据java
A multidimensional array is an array whose components are themselves arrays.
多维数组是一个数组,其组件本身就是数组。 This is unlike arrays in C or Fortran.
这与C或Fortran中的数组不同。 A consequence of this is that the rows are allowed to vary in length.
其结果是允许行的长度变化。
Take int a[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}
取
int a[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}
Here you can't say [3][4] array,but can say it has 3 elements and each element has its own 4 elements. 这里你不能说[3] [4]数组,但可以说它有3个元素,每个元素都有自己的4个元素。
Take int a[][]={{1},{5,6,7},{9,10,11,12}}
取
int a[][]={{1},{5,6,7},{9,10,11,12}}
So here you can say array has 3 elements and each element has different number of elements. 所以在这里你可以说数组有3个元素,每个元素都有不同数量的元素。
I just did this test on an Android environment, which will probably work for this case scenario (I think) 我刚刚在Android环境中进行了此测试,这可能适用于这种情况(我认为)
int a[][] = { { 1 }, { 5, 6, 7 }, { 9, 10, 11, 12 } };
for (int i = 0; i < a.length; i++) {
Log.i("", "i: " + i);
for (int j = 0; j < a[i].length; j++) {
Log.i("", "j: " + j);
}
}
What I got was: 我得到的是:
i: 0
j: 0
i: 1
j: 0
j: 1
j: 2
i: 2
j: 0
j: 1
j: 2
j: 3
So, a
is not int[3][4]
. 所以,
a
不是int[3][4]
。
It's not [3][4]. 这不是[3] [4]。
Its array of array where external array is of dimension "3" and array elements, which are again array, of varying length ie 1, 3 and 4 respectively. 其阵列 ,其中外部阵列维数“3”和阵列元素,这是再一次阵列,分别具有不同长度,即1,3和4的阵列 。
No, it is not. 不它不是。 When you go like
int[][] test = new int[3][4]
that's just syntactic sugar. 当你像
int[][] test = new int[3][4]
那样只是语法糖。 There's nothing magical about a double array. 双阵列没什么神奇的。
Both examples are arrays of arrays. 两个示例都是数组的数组。 Your first happens to have all of those arrays the same legnth ,your second does not.
你的第一个碰巧让所有这些阵列都具有相同的成功,而你的第二个则没有。
As a side comment, the vast majority of java programmers use int[][] a
notation, rather than int a[][]
notation. 作为旁注,绝大多数java程序员使用
int[][] a
表示法,而不是int a[][]
表示法。
There are no 2-dimensional arrays in Java. Java中没有二维数组。
There only exist arrays of arrays, where each array can have a different length . 只存在数组数组, 其中每个数组可以具有不同的长度 。
For matrixes, consider a linear memory layout, and using one of the existing libraries. 对于矩阵,请考虑线性内存布局,并使用其中一个现有库。
Note that the following is valid in Java: 请注意,以下内容在Java中有效:
int[][] example = new int[2][2];
example[0] = new int[]{1,2,3,4,5,6,7,8,9,10};
while a proper matrix module will not allow this to happen. 而适当的矩阵模块不允许这种情况发生。
This is both a strength and a weakness . 这既是优点,也是弱点 。 There are applications where you just need arrays-of-arrays.
有些应用程序只需要数组数组。 And there are cases where it is more efficient (eg storing half a triangular matrix only).
并且存在更有效的情况(例如,仅存储半个三角形矩阵)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.