Java:二维数组以列主要顺序或行主顺序存储? [英] Java: A two dimensional array is stored in column-major or row-major order?

查看:187
本文介绍了Java:二维数组以列主要顺序或行主顺序存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,是一个以列主要或行主要顺序存储的多维数组吗?

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

推荐答案

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

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

int[][]

...是一个 int [] 的数组(当然还有 int [] 是一个 int 的数组。

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

因此,Java既不是列主要也不是行 - 主要订单(但请参阅下面有关如何阅读 a [2] [3] 的说明),因为当给定数组的条目存储在连续的内存块中时,这些条目指向的从属数组是对完全独立的,不相关的内存块的对象引用。这也意味着Java的数组数组本身就是锯齿状的 [0] 中的条目可能指的是一个3槽阵列,一个位于 [1] 可能指的是4个插槽的数组, [2] 可能根本不会引用数组(它可能有 null ),也许 [3] 指的是一个6插槽阵列。

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.

图片价值1k-24字以及所有:

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


                         +−−−−−−−−+
                   +−−−−>| 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 |
                         +−−−−−−−−+

一旦你知道,你知道(比方说) a [2] [3] 表示获取的索引 2 条目引用的数组a ,然后获取该下级数组的索引 3 引用的条目。我认为它与行主要订单非常相似,但它并不完全相同。

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

这篇关于Java:二维数组以列主要顺序或行主顺序存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆