Vector implements a dynamic array. It is to Array List, but with two differences: Vector is synchronized, and it contains many legacy methods that are not part of the collections frame work.With the release of Java 2,Vector wast engineered to extend Abstract List and implement the List interface, so it now is fully compatible with collections

Here are the Vector constructors:

Vector( )
Vector(int size)
Vector(int size, int incr)
Vector(Collection c)

The "first form creates a default vector, which has an initial size of 10. The second form creates a vector .hose initial capacity is specified by size..The third form creates a" vector reinitialize capacity is specified by size and whose increment is specified by " int. The increment specifies the number of elements to allocate each time that a vector is resided upward. The fourth form creates a vector that contains the elements of collection c, This constructor was added by Java 2.

All vectors start with an initial capacity. After this initial capacity is reached, the next time that you attempt to store an object in the vector, the vector automatically allocates space for that object plus extra room for additional objects. By allocating more than just the required memory] the vector reduces the number of allocations that must " take place. This reduction is important, because allocations are costly in terms of time. The amount of extra space allocated during each reallocation is determined by the increment that you specify when you create the vector, If you don't specify an increment, the vector's size is doubled by each allocation cycle. Vector defines these protected data members:

The increment value is stored in capacity Increment. The..number of elements currently in the vector is stored in element Count. The array that holds the vector is stored in. element Data:

In addition to the collections methods defined by List, Vector defines several legacy . methods, which are shown in Because Vector implements List, you can.use a vector just like you.use an Array List instance. You can also manipulate one using its legacy methods. For dimple, after you instantiate a Vector, you can add an element to it by calling add Element(). To obtain , the element at a specific location, call element At( ). To obtain the first element in the vector, call first Element( ). To retrieve the last element, call last Element(). You can obtain the index of an element by using index Of( ) and last Index Of("). to remove an element, cull remove Element(.) or remove Element At(). The following program uses a vector to store various types' of numeric objects. It demonstrates several of the legacy methods defined by Vector. It also demonstrates

Share This