Comparing Vector with ArrayList
ArraryList_Vector1.htm | ||
ArrayList (raw) , ArrayList<E> (Generics) | ||
|
||
Arrays or ArrayList? Programmers are frequently faced with the choice of using a simple array or an ArrayList. If the data has a known number of elements or small fixed size upper bound, or where efficiency in using primitive types is important, arrays are often the best choice. However, many data storage problems are not that simple, and ArrayList (or one of the other Collections classes) might be the right choice. | ||
Automatic expansion. Use ArrayList when there will be a large variation in the amount of data that you would put into an array. Arrays should be used only when there is a constant amount of data. For example, storing information about the days of the week should use an array because the number of days in a week is constant. Use an array list for your email contact list because there is no upper bound on the number of contacts. | ||
Objects only. A possible disadvantage of ArrayList is that it
holds only object types and not primitive types (eg, |
||
Implementation. ArrayLists are implemented with an underlying array, and when that array is full and an additional element is added, a new, larger, array is allocated and the elements are copied from the old to the new. Because it takes time to create a bigger array and copy the elements from the old array to the new array, it is a slightly faster to create an ArrayList with a size that it will commonly be when full. Of course, if you knew the final size, you could simply use an array. However, for non-critical sections of code programmers typically don't specify an initial size. | ||
Comparing Vector with ArrayList
|
||
Vector | ArrayList | |
Synchronized |
|
|
Stretchable (capacity) |
|
|
increases by double or two fold | increases by 50 per cent | |
For an unknown case, Vector is better logically. | ||
Adding in the middle |
|
|