什么是密集数组? [英] What exactly is a dense array?

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

问题描述

我从几页读到的对密集数组的解释似乎相互矛盾.我需要一些帮助来理解它是什么.

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.

但此声明取自 JavaScript 指南Mozilla 开发者网络 (MDN) 说:

由于数组的长度可以随时更改,并且数据可以存储在数组中不连续的位置,因此 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"与sparse"相对,一般在谈论存储时使用.例如,这个数组是密集的:

"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个位置undefined后面跟着100000000.相反,它只是说第 100000000 个是 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天全站免登陆