Both ArrayList and Vector implement the List interface, but Vector has a bunch of other methods that duplicate the functionality of those List methods, but have uglier names: for example, addElement(). Vector also provides a methat that returns an old-fashioned Enumeration are well as an Iterator.
The one additional difference is that the algorithms used to grow the underlying array when elements are added is a little different. Vector's will use, on average, a lot more unused space, but may be a little but faster under certain circumstances.
As I recall according to the Sun documentation on Collections you should use ArrayList in favor of Vector. It's meant as a replacement but Vector will live on at least for the forseeable future in order to meet a binary compatibility requirement. I say "as I recall" because I'm 99% sure of this but I haven't gone back and looked to ensure my memory is accurate, so take it for what it's worth.
Let's say you have an array of size 1, and you're going to add some indeterminate number of elements to it, one after another. Whenever the array is full, you need to allocate a new larger one and copy the old elements into it. This copying is computationally expensive, so you want to do it as infrequently as possible. On the other hand, the only way to reduce the number of allocate/copy cycles is to make bigger allocations, which uses more space: so it's a classic space/speed tradeoff.
Vector doubles the size of the array each time it does a reallocation. Therefore on the average, it uses 25% more memory than it needs. ArrayList, on the other hand, grows the array by a fixed chunk size. Therefore, the amount of extra storage is fixed, and generally much smaller for large Lists.
But note that Vector only has to allocate/copy ln(N) times to populate a collection of size N, whereas ArrayList has to do it N/(chunksize) times. Therefore, if adding a very large but unknown number of elements to a collection can be the performance-limiting step in an application, Vector could, theoretically, perform better. In practice, this advantage may never really be observed.
Then it's only going to be noticed if you're adding a large number of elements. Even then only if the developer does not or cannot use ensureCapacity() to grow the ArrayList once before it adds a big chunk of elements. I would humbly suggest that while correct it might be better to not mention it as it might mislead the already confused into thinking Vector will provide them better performance at the cost of memory.
Originally posted by Ken Blair: I would humbly suggest that while correct it might be better to not mention it as it might mislead the already confused into thinking Vector will provide them better performance at the cost of memory.
I agree about confusing people, but I'm damned if I do, and damned if I don't. If I didn't mention it, Jim would come along behind me and point out that I forgot this essential difference between the two classes.
Originally posted by Ken Blair: When is Vector faster Ernest?
Both Vector and ArrayList have their own "reallocate, copy, store" (better name?) algorithm that is hard-coded into the type. In theory, one could create a scenario that takes advantage of the Vector algorithm such that a performance improvement could be observed. Wouldn't it be nice if users could provide the algorithm strategy for themselves? ...since such an algorithm is not intrinsic to the requirement of "a sequentially ordered list of elements", which is the single role that ArrayList/Vector attempt to achieve (but as per usual J2SE API Specification, over-achieve).