Java:二维数组是按列优先顺序还是按行优先顺序存储?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6630990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:23:57  来源:igfitidea点击:

Java: A two dimensional array is stored in column-major or row-major order?

javamemorymultidimensional-arrayperformance

提问by Zouzias

In Java, is a multidimensional array stored in column-major or row-major order?

在 Java 中,多维数组是按列优先顺序还是按行优先顺序存储?

采纳答案by T.J. Crowder

Java doesn't have multi-dimensional arrays. It has arrays of arrays. So for instance,

Java 没有多维数组。它有数组的数组。所以例如,

int[][]

...is an array of int[](and of course int[]is an array of int).

...是一个数组int[](当然int[]是一个数组int)。

Consequently, Java is neither column-major nor row-major order (but see note below about how to read a[2][3]), because while a given array's entries are stored in a contiguous block of memory, the subordinate arrays those entries point to are object references to completely separate, unrelated blocks of memory. This also means that Java's arrays of arrays are inherently jagged: The entry at [0]might refer to a 3-slot array, the one at [1]might refer to a 4-slot array, [2]might not refer to an array at all (it could have null), and perhaps [3]refers to a 6-slot array.

因此,Java 既不是列优先顺序也不是行优先顺序(但请参阅下面关于如何读取的注释a[2][3]),因为虽然给定数组的条目存储在连续的内存块中,但这些条目指向的从属数组是对完全独立的、不相关的内存块。这也意味着 Java 的数组数组本质上是锯齿状的:at 条目[0]可能指的是 3 槽数组,at条目[1]可能指的是 4 槽数组,[2]可能根本不指数组(它可能有null),可能[3]是指 6 槽阵列。

A picture is worth 1k-24 words and all that:

一张图片值 1k-24 个字,所有这些:

                         +????????+
                   +????>| int[]  |
+???????????+      |     +????????+
|  int[][]  |      |     | 0: int |
+???????????+      |     | 1: int |
| 0: int[]  |??????+     | 2: int |
| 1: int[]  |??????+     +????????+
| 2: null   |      |
| 3: int[]  |??+   |     +????????+
+???????????+  |   +????>| int[]  |
               |         +????????+
               |         | 0: int |
               |         | 1: int |
               |         | 2: int |
               |         | 3: int |
               |         +????????+
               |
               |         +????????+
               +?????????| int[]  |
                         +????????+
                         | 0: int |
                         | 1: int |
                         | 2: int |
                         | 3: int |
                         | 4: int |
                         | 5: int |
                         +????????+

Once you know that, you know that (say) a[2][3]means "Get the array referenced by the entry at index 2of a, then get the entry referenced by index 3of that subordinate array." I think of it as fairly similar to row-major order, but it's not quite the same thing.

一旦你知道这一点,你就知道(比如说)a[2][3]意味着“获取索引2a的条目引用的3数组,然后获取该下级数组的索引引用的条目。” 我认为它与行优先顺序非常相似,但它并不完全相同。

回答by Andy Thomas

Neither. What we may sometimes think of as two-dimensional array in Java is actually an array of references to arrays. It's not stored linearly in memory.

两者都不。我们有时可能认为 Java 中的二维数组实际上是对数组的引用的数组。它不是线性存储在内存中。

The Java Language specification notes this in the introduction:

Java 语言规范在介绍中指出了这一点:

The language supports arrays of arrays, rather than multidimensional arrays.

该语言支持数组数组,而不是多维数组。

This has several implications.

这有几个含义。

  • Arrays of arrays can be jagged-- member arrays can have different lengths.
  • The members of an outer array are references, and can be null.
  • Cloning an outer array is shallow -- the member arrays are shared between the original and the clone.
  • 数组的数组可以是锯齿状的——成员数组可以有不同的长度。
  • 外部数组的成员是引用,可以为空。
  • 克隆外部数组很浅——成员数组在原始数组和克隆之间共享。

From the JLS, section 10.2, "Array Variables":

来自JLS,第 10.2 节,“数组变量”

A single variable of array type may contain references to arrays of different lengths, because an array's length is not part of its type.

数组类型的单个变量可能包含对不同长度数组的引用,因为数组的长度不是其类型的一部分。

From the JLS, section 10.7, "Array Members":

来自JLS,第 10.7 节,“数组成员”

A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

多维数组的克隆是浅的,也就是说它只创建一个新数组。子数组是共享的。

回答by Kal

In Java, you only have one dimensional arrays.

在 Java 中,您只有一维数组。

2D arrays are basically just one dimensional arrays of one dimensional arrays.

二维数组基本上只是一维数组的一维数组。

int[ ][ ] table;

table = new int[3][ ];

table[0] = new int[5];

table[1] = new int[5];

table[2] = new int[5];