Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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.Arrays 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.

like image 763
Margus Avatar asked Oct 03 '10 20:10

Margus


People also ask

What are the major advantages and disadvantages of array?

In an array, accessing an element is very easy by using the index number. The search process can be applied to an array easily. 2D Array is used to represent matrices. For any reason a user wishes to store multiple values of similar type then the Array can be used and utilized efficiently.

What is the disadvantage of array data structure first in last out?

What are the disadvantages of arrays? Explanation: Arrays are of fixed size. If we insert elements less than the allocated size, unoccupied positions can't be used again. Wastage will occur in memory.


2 Answers

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>[]
like image 75
Péter Török Avatar answered Oct 04 '22 02:10

Péter Török


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.

like image 29
Stephen C Avatar answered Oct 04 '22 04:10

Stephen C