Which comes first in a 2D array, rows or columns?

在创建二维数组时,如何记住是否首先指定行或列?


Java is considered "row major", meaning that it does rows first. This is because a 2D array is an "array of arrays".

For example:

int[ ][ ] a = new int[2][4];  // Two rows and four columns.

a[0][0] a[0][1] a[0][2] a[0][3]

a[1][0] a[1][1] a[1][2] a[1][3]

It can also be visualized more like this:

a[0] ->  [0] [1] [2] [3]
a[1] ->  [0] [1] [2] [3]

The second illustration shows the "array of arrays" aspect. The first array contains {a[0] and a[1]} , and each of those is an array containing four elements, {[0][1][2][3]} .

TL;DR summary:

Array[number of arrays][how many elements in each of those arrays]

For more explanations, see also Arrays - 2-dimensional.


While Matt B's may be true in one sense, it may help to think of Java multidimensional array without thinking about geometeric matrices at all. Java multi-dim arrays are simply arrays of arrays, and each element of the first-"dimension" can be of different size from the other elements, or in fact can actually store a null "sub"-array. See comments under this question


Instinctively one thinks geometrically: horizontal (X) axis and then vertical (Y) axis. This is not, however, the case with a 2D array, rows come first and then columns.

Consider the following analogy: in geometry one walks to the ladder (X axis) and climbs it (Y axis). Conversely, in Java one descends the ladder (rows) and walks away (columns).

链接地址: http://www.djcxy.com/p/21318.html

上一篇: 什么是C#5,它来自哪里?

下一篇: 二维数组,行或列首先出现在哪里?