您在這裡

Arrays vs. Lists

15 一月, 2016 - 09:02
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

In no particular order...

  • Arrays:
    • Fast access to all elements.
    • Fixed number of elements held.
    • Difficult to insert elements.
    • Can run into problems with uninitialized elements.
    • Minimal safety for out-of-bounds indices.
    • Minimal memory used
    • Simple syntax
    • Must use procedural techniques for processing.
    • Often incompatible with OO architectures.
    • Difficult to prove that processing algorithms are correct.
    • Processing algorithms can be very fast.
    • Processing algorithms can be minimally memory intensive
  • Lists:
    • Slow access except to first element, which is fast.
    • Unlimited number of elements held.
    • Easy to insert elements.
    • Encountering uninitialized elements very rare to impossible.
    • Impossible to make out-of-bounds errors.
    • Not optimized for memory usage.
    • More cumbersome syntax.
    • Can use OO and polymorphic recursive techniques for processing.
    • Very compatible with OO architectures.
    • Easy to prove that processing algorithms are correct.
    • Processing algorithms can be quite fast if tail-recursive and using a tail-call optimizing compiler.
    • Processing algorithms can be very memory intensive unless tail-recursive and using a tail-call
    • optimizing compiler.
Note: Arrays are optimized for size and random access speed at the expense of OO design and recursion. If you do not need speed or low memory, do not use an array. If you must use an array, tightly encapsulate it so that it does not affect the rest of your system.