0% found this document useful (0 votes)
1 views

Vector Class in Java

The Vector class in Java is a growable array of objects that is part of the java.util package and implements the List interface. It is thread-safe, allows null elements, and supports enumeration for backward compatibility. The class includes various constructors and methods for adding, removing, and accessing elements, as well as managing the vector's capacity.

Uploaded by

sahilshedke
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Vector Class in Java

The Vector class in Java is a growable array of objects that is part of the java.util package and implements the List interface. It is thread-safe, allows null elements, and supports enumeration for backward compatibility. The class includes various constructors and methods for adding, removing, and accessing elements, as well as managing the vector's capacity.

Uploaded by

sahilshedke
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Vector Class in Java

The Vector class implements a growable array of objects. Vectors fall in legacy classes,
but now it is fully compatible with collections.

Package:- java.util package and implement the List interface.

 Thread-Safe: All methods are synchronized, making it suitable for multi-threaded


environments. However, this can lead to performance overhead in single-threaded
scenarios.
 Allows Nulls: Can store null elements.
 Enumeration Support: Provides backward compatibility with Enumeration, a legacy
way of iterating over elements.
Vector implements a dynamic array that can hold objects of any type and any number. The
objects do not have to be homogeneous.
Array also implemented as vector
Vector Implementation and Declaration:
1. Vector vector_name = new Vector(); //declaration of vector with default constructor of Vector
class
2. Vector vector_name= new Vector(size); //declaration of vector with specified size parameter
3. Vector vector_name= new Vector(int size,int incr); //creation of vector with specified size
and whether it grows with incr parameter

Vector Constructor:-
1. Vector():- Creates a new empty vector size 10 and capacity increment 0
ex. Vector v = new Vector();

2. Vector(int Initial Capacity) : Creates a new empty vector with specified capacity and
with capacity increment 0
ex. Vector v = new Vector(20);

3. Vector(Collection c) : Creates new vector containing elements of specified collection


Ex. Vector v = new Vector(myCollection);

4. Vector(int Initial_Capacity, int Capacity_Increment) : Create new empty Vector with


specified vector capacity and with specified Capacity increment.
Ex. Vector v = new Vector(100,20);
Methods of Vector Class:

Sr. Method Description Syntax


No
1 List.addElement(item) Add the item into the list Vector addElement(Object
at the end item)
2 List.elementAt(n) Gives the name of the String elementAt(int n)
Nth (index) object
3 List.size() Gives the number of int size()
objects in vector
4 List.removeElement(item) Remove the specified Vector
item from list removeElement(Object
item)
5 List.ElementAt(n) Remove the item stored Vector
at Nth index of vector removeElementAt(Object
item)
6 List.removeAllElements() Remove all elements of Vector
vector removeAllElements(Object
item)
7 List.copyInto(array) Copies all items from Void copyInto(String arr[])
vector to array of string
type.
8 List.insertElementAt(item,n) Insert Element into Vector
vector at Nth index insertElementAt(object
item,int n)
9 List.capacity() Returns the capacity of Int capacity()
the vector
10 List.firstElement() Returns the first element Vector firstElement()
of the vector
11 List.lastElement() Returns the last element Vector lastElement()

You might also like