Vector and ArrayList Comparisons
Sometimes it is better to use a vector, sometimes ArrayList better, not accurate and easy to give the answer, because depending on the circumstances, there are four main factors to consider:
1:api
2: Sync (synchronization)
3: Data Growth (growth)
4: Use mode (Usage pattern)
The following separate meanings are discussed:
1:api
In the description of the Java program language of Ken Arnold, James Gosling, and David Holmes, vector and ArrayList are analog designs, and from the API point of view, two classes have many similarities, but two classes are somewhat different.
2: Sync (synchronization)
From the point of view of synchronicity: vector is synchronized, some methods of accessing vector content are thread-safe, while ArrayList is asynchronous, the method of accessing ArrayList content is thread-unsafe, because there is this difference, Modified with the SYNCHRONIZED keyword will degrade performance, so if you don't need a thread-safe collection, use ArrayList. There is no need to spend unnecessary synchronization performance overhead.
3: Data Growth (growth)
From the internal data structure, ArrayList and vectors use array patterns to store content, and you need to be careful about the nature of programming, when inserting data in ArrayList and vectors, if ArrayList or Vector objects are stored beyond the corresponding space (that is, the internal number of the group leader degree). The corresponding ArrayList or vectors will expand the array within them. Vector by default expands the internal array by one time, which is equivalent to the size of two previous arrays, and ArrayList only increases the size of 50%, depending on which class you use to determine the performance impact of adding an element, the best way is to set the initialization capacity of the object to the maximum required capacity, This avoids the growth of the element after you insert it later. (since growth involves copying elements within an array into a newly created array), if you don't know how much data will grow, but you know the rate at which the data is growing, vectory can have a slight advantage because you can set the value of growth.
As for why the vector is set to sync and grow to one of its own size, ArrayList is set to be asynchronous and grow to half its size:
Gossip about: Set to sync indicates that the data growth is more intense, that is, the growth rate and frequency is large, if set to half, soon the array again full, so set to one times.
and arraylist corresponding growth rate is slow, set to a different step, not easy to appear multithreading concurrency problem.
Gossip is just a personal understanding, welcome to Pat Bricks.
4: Use mode (usage pattern)
ArrayList and vectors are all better at retrieving the elements of a particular location and adding and removing elements at the end of the collection, all of which are in the linear time of O (1), But adding and removing elements (not at the end) of some other position requires a more expensive linear time overhead of O (n-i), n represents the total number of elements, I represents the index position to insert or remove, and these operations are more costly because you have to move all the elements on the back end of the index I, what does that mean?
This means that you first look for elements that are indexed as well as I, and then insert and remove the element at the end of the array. If you want to do inserts or deletes, some other collection classes can be considered, such as: LinkedList can add or remove elements of some position within the constant time O (1), but the location of the index element is slow, the time cost of O (i) is required, I is the index of the element that is required, Traversing ArrayList is easy because you can simply use the index instead of creating an iterator, LinkedList also creates an internal object for each inserted element, so you must be aware that the extra garbage is created.
Final statement: You can use the most common array to replace vector or ArrayList, especially because of the performance of standard Code, the use of arrays to avoid synchronization (synchronization), additional method calls, the most modest resizing, you only need to spend additional development time. The array enables you to tailor the collection of requirements to your program.