Vector Class in JAVA

Vector Class in JAVA

Vector is a dynamic array. Vector is a class in the util.paclkage. It is similar to an array but with difference that vector contains methods to insert and delete an element. Once a chunk of memory is allocated for an array , its size will remain fixed unless you reallocate or free the memory. Size of vector is not fixed. If the allocated storage in the vector is too small to insert elements, a new array is allocated, all elements are copied or moved to the new array, and the old array is freed.

Constructor:

  1. Vector(): Creates a default vector of initial capacity is 10.
  2. Vector(int size): Creates a vector whose initial capacity is specified by size.
  3. Vector(int size, int incr): Creates a vector whose initial capacity is specified by size and increment is specified by incr. It specifies the number of elements to allocate each time that a vector is resized upward.
  4. Vector(Collection c): Creates a vector that contains the elements of collection c.
Method Description
capacity() Returns the current capacity of the vector
addElement() Inserts an element specified in the brackets at the end of the Vector
removeElement() Deletes the element specified in the brackets
removeElementAt() Deletes the element at the index specified in the brackets
elementAt() Returns the element at the index specified in the brackets
firstElement() Returns the first element in the Vector
lastElement() Returns the last element in the Vector
indexOf() Returns the index of the element specified in the brackets
lastIndexOf() Returns the last index of the element passed to it in the brackets

 

Share Me!
  •  
  •  
  •  
  •  
  •  
  •