Java中的内存中如何表示二维数组? [英] How is a two-dimensional array represented in memory in java?

查看:89
本文介绍了Java中的内存中如何表示二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用键,值对实现数据结构,并正在研究数组实现。

I am trying to implement a data structure with Key, Value pairs and was looking at array implementations.

实现此目标的一种方法是声明单独的1-D

One way of achieving this is to declare seperate 1-D arrays for Key and Values.

  private int[] keys = new int[N];
private int[] values = new int[N];

但是可以通过声明如下的二维数组而不损害数据局部性来实现相同的效果?

But can the same be achieved by declaring a 2-D array as follows and not compromise on data locality?

private int[][] keysAndValues = new int[2][N];

在这里,Java以行优先顺序实现多维数组似乎很重要?以这种方式声明数组是否有任何性能优势,或者这会使代码的可读性降低?

It seems important here that Java implements multidimensional arrays in row-major order? Are there any performance advantages in declaring the array this way, or does this make the code less readable?

推荐答案

Java实际上是对象引用的数组,每个对象引用都指向一维数组。 2D数组以及每个1D数组都是独立的堆对象,并且(理论上)可以位于堆中的任何位置。

A 2D array in Java is actually an array of object references, each of which points to a 1D array. The 2D array, and each of the 1D arrays are all separate heap objects, and could (in theory) be anywhere in the heap.

(有关原因的讨论,参见:为什么Java没有真正的多维数组?

(For a discussion of why, see: Why doesn't Java have true multidimensional arrays?)


但是通过声明二维数组可以实现相同的效果

But can the same be achieved by declaring a 2-D array as follows and not compromise on data locality?

是的。

这两个版本之间的数据局部性差异很小,特别是如果我们可以假设 N 2 相比是很大的话。 (如果不能这样做,那么数据局部性很可能是不相关的;即,性能差异将太小而无明显意义。)

There is minimal difference in data locality between the two versions, especially if we can assume that N is large compared with 2. (And if we can't, then data locality is most likely irrelevant; i.e. the performance difference will be too small to be significant.)


在这里,Java以行优先顺序实现多维数组似乎很重要?

It seems important here that Java implements multidimensional arrays in row-major order?

这是一个问题吗?如果是的话,我想是的。当然,这是相关的……尽管如果Java将它们实现为以列为主,那么您只需翻转行和列并获得等效的解决方案

Is that a question? If yes, then I guess so. It is certainly relevant ... though if Java implemented them column-major, then you would simply flip the rows and columns and get an equivalent solution


以这种方式声明数组是否有性能优势,或者这会使代码的可读性降低?

Are there any performance advantages in declaring the array this way, or does this make the code less readable?

性能问题是可能无关紧要。但是,如果真的真的很重要,那么最好的建议是为您自己配置和优化代码……在REAL输入数据集上。

The performance issues are likely to be insignificant. But if it really, really matters then the best advice is to profile and optimize the code for yourself ... on REAL input datasets.

至于可读性,这就是给你判断。我无法预测您的代码将是什么样。

As for readability, that is up to you to judge. I can't predict what your code is going to look like.

如果您真的想控制内存位置,那么最好的方法是使用单个一维数组,并以整体上为您提供最佳局部性的方式映射索引。 (这将取决于您的应用程序以及它如何引用数组中的数据。)

If you really want to control the memory locality, then the best way is to use a single 1D array, and map the indexes in a way that gives you the best locality overall. (That will depend on your application and how it references the data in the array.)

这篇关于Java中的内存中如何表示二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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