vector in java

The vector class in java is inherited by list interface of java collection framework. It was introduced in 1.0 version so it is also called native class. The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

As of the Java 2 platform v1.2, this class was retrofitted to implement the interface,List making it a member of the Java Collections Framework. Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use in ArrayList place of Vector

Introduction of Vector

  1. The underlying data structure is a resizable array.
  2. Duplicates are allowed.
  3. Insertion order preserved.
  4. Null insertion is possible.
  5. Heterogeneous objects are allowed.
  6. It implements serializable, clonable and RandomAccess Interface.
  7. Most of the methods are synchronized hence vector objects are thread safe.
  8. It is the best choice if our frequent operation is the retrieval of data.

Methods of vector:

For the better layout, look in desktop mode.
    1. For adding objects
Add(Object o);                             -- from collection
Add(int index, Object o);               -- from list
addElement(Object o);                  -- from vector
    1. For accessing elements
Object get(int index)                           -- from collection
Object elementAt(int index)                   -- from vector
Object FirstElement()                             -- from vector
Object LastElement()                             -- from vector
  1. Other methods
Int size();
Int capacity();
Enumeration elements();

click on the below image to learn all the methods available in the vector.
vector methods

Constructors in vector:

  1. Vector v = new vector()
    Creates an empty vector with default initial capacity 10.
New capacity = 2 * current capacity.
  1. Vector v = new vector(int initial_size);
Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.
  1. Vector v = new vector(int initial_size , int incrementalCapacity);
Constructs an empty vector with the specified initial capacity and capacity increment.
  1. Vector v = new vector(Collection c);
Constructs a vector containing the elements of the specified collection, in the order, they are returned by the collection's iterator.

For more - Read the official documentation

Comments