What is the difference between a Vector and an Array. Discuss the advantages and disadvantages of both?
The vector container class generalizes the concept of an ordinary C array. Like an array, a vector is an indexed data structure, with index values that range from 0 to one less than the number of elements contained in the structure. Also like an array, values are most commonly assigned to and extracted from the vector using the subscript operator. However, the vector differs from an array in the following important respects:
The size of the vector can change dynamically. New elements can be inserted
on to the end of a vector, or into the middle. It is important to note,
however, that while these abilities are provided, insertion into the middle
of a vector is not as efficient as insertion into the middle of a list.
A vector can only hold references to objects and not primitive types.
Vector Implementaions are usually slower then array because of all the functionality that comes with them. As implemented in Java, vector is a thread-safe class and hence all methods are synchronous methods, which makes them considerably slow.
ArrayList has no default size.
Vector has a default size of 10.
The Output is:
ArrayList Intial Size : 0
The default capacity of vector is 10.
Do you have a Java Problem?
Return to : Java Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.