简体   繁体   English

在带有Vectors的Flash Player 10中,为什么还要使用Arrays呢?

[英]In Flash Player 10 with Vectors, why should you use Arrays anymore?

Is there a reason to stick to Arrays as the default list data structure in AS3 if you are targeting Flash Player 10? 如果您的目标是Flash Player 10,是否有理由坚持使用Arrays作为AS3中的默认列表数据结构? Why not use Vectors (typed arrays) as default throughout your program since they are: 为什么不在整个程序中使用Vectors(类型化数组)作为默认值,因为它们是:

  • faster 快点
  • type checked 类型检查

Does it perform badly or incur higher memory overheads? 它是否表现不佳或导致更高的内存开销? Any reason to use arrays anymore? 有没有理由再使用数组了?

Plenty of reason. 很多理由。 Vectors are not sparsely populated, for example, so that if your Vector has an index of 999, you have an array of 1,000 elements. 例如,向量不是稀疏填充的,因此如果您的Vector的索引为999,则您有一个包含1,000个元素的数组。 In a standard Array, you could have as few as one. 在标准阵列中,您可以只有一个。

I'm answering my own question based on what I learnt of the issue. 我根据我对这个问题的了解回答了我自己的问题。

Vectors are faster for these datatypes ONLY: -- ( proof ) 这些数据类型的矢量更快: - ( 证明

  • int INT
  • uint UINT
  • Number
  • Boolean 布尔

Arrays are preferable for all other types: 对于所有其他类型, 数组受欢迎

  • Strings 字符串
  • classes

Vectors are more limited to work with: 向量更受限于使用:

  • reading/writing is bounds checked 阅读/写作受到限制
    • non-existent slots cannot be set directly with [5] = Val 不存在的槽不能用[5] = Val直接设置
    • non-existent slots cannot be read (or you'll get an Exception) 无法读取不存在的插槽(或者您将获得异常)
  • must push() to create slots 必须push()才能创建插槽
  • sorting is slower 排序速度较慢

Vectors are more troublesome: 向量更麻烦:

  • multi-D arrays cannot be convert to a vector 多D数组无法转换为向量
  • vector cannot be converted to a multi-D array 矢量无法转换为多D数组
  • cannot work with String.split() 无法使用String.split()
  • fixed type for all elements - unusable for JSON 所有元素的固定类型 - 对JSON不可用

Vectors can be easier to debug: 矢量可以更容易调试:

  • setting a [Vector of ints] into a [Vector of Vector of ints] will throw an Exception 将[向量的向量]设置为[向量向量的向量]将抛出异常
  • compile time errors (in some cases) 编译时错误(在某些情况下)
    • when trying to set a [Vector of ints] into a [Vector of Strings] 当试图将[向量的向量]设置为[向量的字符串]
    • when trying to set a [Vector of ints] into a [Vector of Vector of ints] 当试图将[向量的向量]设置为[向量的向量的向量]

Maybe specially when you don't know the type - it's a loosely based list, in a way. 也许特别是当你不知道这种类型时 - 在某种程度上它是一个基于松散的列表。 Like, say, with JSON data. 就像说,使用JSON数据。

Edit : oh, here's another semi-reason - String.split() . 编辑 :哦,这是另一个半原因 - String.split() That'll return you an Array of strings. 这将返回一个字符串数组。 Dunno why you can't get a Vector.<String> out of it, grr. 不知道为什么你不能得到一个Vector.<String>了,grr。

With that said, in Flash 10+, 99% of the time you'll be using Vectors instead. 话虽如此,在Flash 10+中,99%的时间你会使用Vector。 All 'disadvantages' of Vectors are just for very specific (often rare) use cases. Vector的所有“缺点”仅适用于非常具体(通常很少见)的用例。

As Robusto already said, vectors are not sparsely populated. 正如Robusto已经说过的那样,载体的人口稀少。 Although this might be bad for for size, it is very-very good for speed. 虽然这可能对尺寸不利,但它对速度非常有利。 So if you don't need very sparse populated structures (and that to implement that sparse thing also takes some extra space), you can just take advantage of the speed. 因此,如果您不需要非常稀疏的填充结构(并且实现稀疏的东西也需要一些额外的空间),您可以利用速度。

It is the typical trade-of: memory vs. speed :-) 这是典型的交易:记忆与速度:-)

It just depends on what you're trying to do. 这取决于你想要做什么。 Try sorting a Vector? 尝试排序Vector? It'll take much longer. 这需要更长的时间。 Are you going to need to splice an array or vector? 你需要拼接一个数组或向量吗? In which case, you'd probably want to ditch them both and use a linked list. 在这种情况下,你可能想要抛弃它们并使用链表。 If you're looking for something with named keys that you need to reference, then you'd probably use a Dictionary instead. 如果您正在寻找需要引用的命名键的东西,那么您可能会使用Dictionary。 It's a vague, useless answer--"it depends"--but it really does. 这是一个模糊,无用的答案 - “它取决于” - 但确实如此。 There's a reason why there are so many different data list structures. 有这么多不同的数据列表结构的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM