简体   繁体   中英

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? Why not use Vectors (typed arrays) as default throughout your program since they are:

  • 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. 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
  • 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
    • non-existent slots cannot be read (or you'll get an Exception)
  • must push() to create slots
  • sorting is slower

Vectors are more troublesome:

  • multi-D arrays cannot be convert to a vector
  • vector cannot be converted to a multi-D array
  • cannot work with String.split()
  • fixed type for all elements - unusable for 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.

Edit : oh, here's another semi-reason - String.split() . That'll return you an Array of strings. Dunno why you can't get a Vector.<String> out of it, grr.

With that said, in Flash 10+, 99% of the time you'll be using Vectors instead. All 'disadvantages' of Vectors are just for very specific (often rare) use cases.

As Robusto already said, vectors are not sparsely populated. 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? 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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