Vector or arraylist - Which one is better, why?
To answer this question, it is not possible, sometimes it is better to use the vector; sometimes ArrayList, sometimes these two are not
the best choice. Don't expect to get a simple and affirmative answer, because it depends on what you do. There are 4 consideration below.
the elements of:
l API
l Synchronous processing
l Data growth
l use mode
The following is aimed at these four parties.
API
There is this book in "Java Programming Language" (Addison-Wesley, June 2000) compiled by Ken Arnold.
Description, Vector is similar to arraylist.. All these two classes are very phase [B] from the perspective of the API. But they also
There are some main differences. Synchronization
Vector is synchronous. Some methods in this class ensure that objects in the VECTOR are threaded. And arraylist is next ...
The object in ArrayList is not a thread. Because synchronization requirements affect the efficiency of the execution, if you don't
The collection of thread security is used as a good choice to use ArrayList, which avoids unnecessary brought by synchronization.
Performance overhead.
Data growth
From the internal implementation mechanism, ArrayList and Vector are used to control the objects in the collection (array). When you give these two ± »
When an element is added to the type, if the number of elements exceeds the current length of the internal array, they need to extend the length of the internal array,
VECTOR automatically grows up with a double array length by default, ArrayList is 50% of the original, so the last set you get.
The space occupied is always larger than you actually needs. So if you want to save a lot of data in the collection, use the vector.
Some advantages, because you can avoid unnecessary resource overhead by setting the initialization size of the collection.
Mode of use
In ArrayList and Vector, look up data from a specified location (by index) or increase the end of the collection.
The time spent in an element is the same, this time we use O (1). However, if there is an increase or removal of other locations in the collection
The time taken so that the time will grow in line shape: o (n-i), where n represents the number of elements in the collection, i represents an element increase or removal
The index position of the veins. Why is this so? Those all elements after the group I and I and II elements in the collection of the above operations
To perform the operation of the displacement. All this means »?
This means that you just find the elements of a specific location or only increase the end of the collections, remove the elements, then use the Vector or
ArrayList can be. If you are other actions, you'd better choose another episode. For example, the LinkList set is increasing
Or remove the elements of any location in the collection - O (1), but it is slow in the use of an element in the index.
-O (i), where i is the location of the index. It is also easy to use ArrayList, because you can use the index to replace the creation
The operation of the Iterator object. Linklist also creates an object for each inserted element, all you have to understand that it will also bring additional opening.
pin.
Finally, Peter Haggar is recommended in the "Practical Java" book, it is recommended to use a simple array (array) instead of the Vector
Or arraylist. Especially for programs for performing efficiency requirements> Because the use of array avoids synchronization, additional
Method calls and unnecessary reassignment of operations.