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 VectorIntroduction of Vector
- The underlying data structure is a resizable array.
- Duplicates are allowed.
- Insertion order preserved.
- Null insertion is possible.
- Heterogeneous objects are allowed.
- It implements serializable, clonable and RandomAccess Interface.
- Most of the methods are synchronized hence vector objects are thread safe.
- 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.- For adding objects
Add(Object o);
-- from collectionAdd(int index, Object o);
-- from listaddElement(Object o);
-- from vector- For accessing elements
Object get(int index)
-- from collectionObject elementAt(int index)
-- from vectorObject FirstElement()
-- from vectorObject LastElement()
-- from vector- Other methods
Int size();
Int capacity();
Enumeration elements();
click on the below image to learn all the methods available in the vector.
Constructors in vector:
Vector v = new vector()
Creates an empty vector with default initial capacity 10.
New capacity = 2 * current capacity.
Vector v = new vector(int initial_size);
Vector v = new vector(int initial_size , int incrementalCapacity);
Vector v = new vector(Collection c);
For more - Read the official documentation
Comments
Post a Comment