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:
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.Exceptions and important notes.
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.
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 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.
Since Java5, arrays are very rarely the best way to store data - generic collections are almost always superior:
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)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 messagesArrayList
, but if you need associative, ordered, prioritized, unique-value, thread-safe, immutable etc. storage, you would be hard pressed with an arraycontains
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With