Vector or ArrayList that is the question December 16, 2003 Author: yahoo163 Matrix- dance with Java
Translator: On that day, a good friend asked me what is the difference between Vector and ArrayList, it is not very clear, so I have to take a book, but I have not specifically compared it in the book. Oh, sorry yellow The sky is not worn, I finally found the answer to the question. Here I translated it, I hope to help everyone. There is no place to translate, you are still looking to correct :) Original: http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html? Vector or arraylist - Which one is more Ok, why? To answer this question, it is not possible, sometimes it is better to use 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 four factors to consider below: l APIL Synchronous Processing L Data Growth L Usage Mode The 4 aspects of the 4 aspects are discussed by "Java Programming Language" built by Ken Arnold et al. (Addison-Wesley, June 2000) This book has such a description, Vector is similar to arraylist.. All these two classes are very phase [B] from the perspective of the API. But there is still some main differences between them. Synchronous vector is synchronized. Some methods in this class ensure that objects in the VECTOR are threaded. ArrayList is asynchronous, so objects in ArrayList are not threads. Because synchronization requirements affect the efficiency of the execution, if you don't need a collection of thread security, use arraylist is a good choice, which avoids unnecessary performance overhead due to synchronization. Data growth From the internal implementation mechanism, ArrayList and Vector are used to control the objects in the collection (array). When you add an element to these two types, if the number of elements exceeds the current length of the internal array, they need to extend the length of the internal arrays, and Vector by default, the original double array length is automatically increased, and ArrayList is original. 50%, so the space you get this collection is always bigger than you actually needs. So if you want to save a lot of data in a collection, use the vector has some advantages, because you can avoid unnecessary resource overhead by setting the initialization size of the set. Using the mode In ArrayList and Vector, look up data from a specified location (by index) or increase the time of the set of elements, the time to remove an element is the same, this time we use O (1). However, if the time spent in addition to or removing the elements in the set of other locations, the time is linear: O (N-I), where n represents the number of elements in the collection, i represents an index position of the element to increase or remove elements. Why is this so? The operation of the displacement should be performed in all elements after the collections in the collection of the above operations. What does this mean? This means that you just find the elements of a specific location or only add, remove the elements, so you can use the vector or arraylist. If you are other actions, you'd better choose another collection operation class. For example, the LinkList Collection class is the same - O (1) in the time of increasing or removing any position in any location in the collection, but it is slow -o (i) in the use of an element, where i is The location of the index. It is also easy to use ArrayList because you can simply use the index to replace the operation of creating the Iterator object. Linklist also creates an object for each inserted element, all you have to understand that it will also bring additional overhead.
Finally, in the "Practical Java" book, Peter Haggar recommends using a simple array (array) instead of Vector or ArrayList. Especially for programs with high execution efficiency requirements. Because the array is used to avoid synchronization, additional method calls and unnecessary reassignment operations.