什么是密集阵列? [英] What exactly is a dense array?

查看:104
本文介绍了什么是密集阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从几页读到的密集阵列的解释似乎彼此矛盾。我想帮助理解它是什么。

The explanation for a dense array that I read from a few pages seem to be in contradiction to one another. I'd like some help understanding what it is.

虽然有些链接(搜索结果1 搜索结果2 )建议它只是一个数组,其中:

While some links (search result 1, search result 2) suggest that it simply is an array where:


  1. 数组的元素已知为特定值;并且

  2. 在初始化时被分配给数组。

暗示那里是JavaScript数组是密集的。

The allusion there is that JavaScript arrays are dense.

这一切都有意义,直到这里。

It all makes sense up until here.

但这句话取自Mozilla开发人员网络(MDN)上的 JavaScript指南说:

But this statement taken from the JavaScript Guide on the Mozilla Developer Network (MDN) says:


由于数组的长度可以随时改变,并且数据可以存储在非连续的位置数组,JavaScript数组是
不保证密集;这取决于程序员如何选择
来使用它们。一般来说,这些都是方便的特征;但如果
这些功能不适合您的特定用途,您可以考虑使用类型化数组

Since an array's length can change at any time, and data can be stored at non-contiguous locations in the array, JavaScript arrays are not guaranteed to be dense; this depends on how the programmer chooses to use them. In general, these are convenient characteristics; but if these features are not desirable for your particular use, you might consider using typed arrays.

这让我很困惑。我的问题是:

And this has confused me now. My question is:

MDN页面上的语句表示JavaScript数组不能保证密集时是什么意思?如果这意味着以下不是密集数组,因为在初始化时它的一个或多个元素是 undefined ,那么为什么上面列出的链接似乎表明JavaScript数组确实密集?

What does the statement on the MDN page mean when it says JavaScript arrays are not guaranteed to be dense? If it means that the following is not a dense array because one or more of its elements are undefined at the time of initialization, then why do the links I listed above seem to indicate that JavaScript arrays are indeed dense?

var array = new Array(1, , 3, ); // [1, undefined, 3, undefined]


推荐答案

密集与稀疏相反,通常在谈论存储时使用。例如,此数组是密集的:

"Dense" is in opposition to "sparse", and generally is used when talking about storage. For example, this array is dense:

a = [undefined, undefined, 2]

它可以完全像存储在内存中:三个位置的序列,前两个是 undefined ,第三个是 2

It can be stored in memory exactly like that: a sequence of three locations, the first two being undefined, the third being 2.

此数组稀疏:

a = []
a[100000000] = 100000000

它不会作为100000001个位置的序列存储在内存中,因为它的效率非常低。它肯定不是 100000000 未定义的位置后跟 100000000 。相反,它只是说100000000th是 100000000 ,并且没有空间分配给前100000000个元素。

It is not stored in memory as a sequence of 100000001 locations, as it would be horribly inefficient. It is definitely not 100000000 places of undefined followed by 100000000. Rather, it just says 100000000th one is 100000000, and there is no space allocated to the first 100000000 elements.

(实际上,尝试用 2 而不是 100000000 来做这件事,你会发现一件奇怪的事情:Chrome将会将密集数组显示为 [undefined,undefined,2] ,但稀疏数组显示为 [undefined×2,2] 。)

(Actually, try to do this with 2 instead of 100000000, and you'll notice a curious thing: Chrome will display the dense array as [undefined, undefined, 2], but the sparse one as [undefined × 2, 2].)

这篇关于什么是密集阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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