开发者

What are the weaknesses and shortcomings of arrays? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

I would like to know what are the weaknesses of the arrays. I think that it is very helpful to know in order to determine whether the arrays are the best way to store the data in particular situation, or to predict the time of execution.

Edit 1: Clarification, by arrays as I understand them:

  1. Not finite, but fixed size storage, that is used as sequential data containers, that are mutable. 1 or more indexes are used to reference a specific data container.
    1. Data contained must be of same type, if the type is primitive.
    2. Data contained must be of same data type or descendant of the type, if type is object (this is polymorphism), this kind of type conversion is narrower to wider also called covariant conversion.
    3. All arrays in Java are 1 dimensional arrays.
    4. Swapping elements or finding element with its indexes is fast operation
    5. Adding/Removing elements is slow operation, as array is recreated
    6. Element types are enforced at runtime, this is called arrays are reified.
    7. Java array methods are in java.util.A开发者_如何学Pythonrrays and thy do not even contain basic array manipulation methods like union and intersection. Its sad, that guava-libraries is not part of standard Java.
    8. Object array is filled with references to storage locations where the data actually is.
  2. Array of arrays is called 2 dimensional array.
    1. 2d arrays first array is filled with references to other arrays.
    2. Other arrays are not stored sequentially.
    3. Other arrays can vary in element size, this is called ragged- and jagged arrays.
    4. Java arrays have row-major order.
    5. Array dimension is typically limited to 255, but it varies from implementation.
  3. Exceptions and important notes.

    1. In case of managed memory environment, the absolute locations of data elements can be sequential, but most likely are not.
    2. Arrays used directly can't be threadsafe, immutable or have transactional integrity and because of this, there is no direct way to go parallel.
    3. If you need to find if specific data is contained in sorted array you can use binary search, that is not much slower then hashset.contains what I assume uses VMMemoryManager.getIdentityHashCode, that is basically just old school modulation calculus and where collisions can happen.
    4. Java generics is just compiler magic, but if you want to use Java generic with arrays, then you can't use primitive types.
    5. In Java 6, Collections.sort() works like - dump data in array and ... - at that point difference is x2 times in memory.
    6. Garbage collector really does not like arrays, and thy usually end up in old generation, and wont be freed any soon.
    7. It's a bad idea to enter big data elements in arrays, but i can't remember why.

    In my opinion arrays are one of very best ways to store data, just not directly. At times, my head seems to be like a garbage bin, where knowledge is malformed. This is one reason why I asked this question - to confirm what i know.


Since Java5, arrays are very rarely the best way to store data - generic collections are almost always superior:

  • generic elements (especially with wildcards) bring type safety and flexibility - you can use not only List<T> but List<? extends T> and List<? super T>, as opposed to T[] (which corresponds to the second of the three variants, not the first)
  • subtyping for generics is invariant, which in brief means that e.g. a List<String> is not a List<Object> - this saves you from a lot of runtime exceptions, because the compiler can detect them and give error messages
  • arrays are fixed size, collections can dynamically resize themselves as needed
  • collections come in lots of different flavors, with distinct capabilities, useful for different scenarios - e.g. linked list, queue, map, set. Arrays can be usually directly replaced by ArrayList, but if you need associative, ordered, prioritized, unique-value, thread-safe, immutable etc. storage, you would be hard pressed with an array
  • collections have several useful methods, e.g. contains
  • you can only create arrays of reifiable types - i.e. you can't create a List<String>[]


You ought also to know the strengths of arrays, and there are a couple.

  • Arrays of primitive types typically take significantly less space per element than collections of the corresponding wrapper types. This is partly due to the overhead of the wrapper objects (depending on how they are created), and partly due to the fact that a JVM typically stores booleans, bytes, chars and shorts 2, 4 or 8 to a machine word in an array.

  • Arrays of all types have a smaller per-array space overhead than collection types. For example, an ArrayList is really an array of objects wrapped in another object with 2 fields.

  • Array access and update is a bit faster than the equivalent collection get and set operations.

Now, these space / performance differences are not usually worth worrying about. (The flexibility and convenience of collections outweighs them.) However, in some data intensive applications these differences can be significant.


Probably easiest to compare them against the advantages of alternatives:

vs. ArrayList<T>
Much more convenient to add and remove elements. Excepting adding at the end, most operations just as slow (and fast) as arrays.

vs. LinkedList<T>
Much faster to add and remove elements, extremely slow for getting elements by index.

vs HashSet<T>
Much faster contains, add, and remove operations. Lose element ordering.

vs HashMap<TKey, TValue>
Allows you to quickly access the element by a means other than an index. Lose element ordering.


Overall weaknesses:

  • Fixed length. Length of an array is declared at initialisation, and can't be changed after that.
  • Arrays use int as index. No array can contain more than 2^31 - 1 elements
  • You can't create an immutable array
  • You can't create generic arrays
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜