java.util package.
package java.util;
public class Vector<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
// Constructors
// Methods
// Fields
}
Vector implements the List interface, making it part of the Java Collection Framework (after JDK 1.2).
RandomAccess, so elements can be accessed quickly by index (like ArrayList).
Vector is a growable array that stores elements dynamically, similar to ArrayList.
Vector Class
Vector class:
| Sr. No. | Constructor | Description |
|---|---|---|
| 1 | Vector() |
Constructs an empty Vector with an initial capacity of 10. |
| 2 | Vector(int initialCapacity) |
Constructs an empty Vector with the specified initial capacity. |
| 3 | Vector(int initialCapacity, int capacityIncrement) |
Constructs an empty Vector with the specified initial capacity and capacity increment. |
| 4 | Vector(Collection<? extends E> c) |
Constructs a Vector containing the elements of the specified collection, in the order returned by the collection’s iterator. |
Vector Class
Vector class:
| Sr. No. | Method | Description |
|---|---|---|
| 1 | void addElement(E obj) |
Adds the specified element to the end of the Vector. |
| 2 | void insertElementAt(E obj, int index) |
Inserts the specified element at the specified position in the Vector. |
| 3 | E elementAt(int index) |
Returns the element at the specified index in the Vector. |
| 4 | E firstElement() |
Returns the first element in the Vector. |
| 5 | E lastElement() |
Returns the last element in the Vector. |
| 6 | void removeElementAt(int index) |
Removes the element at the specified position in the Vector. |
| 7 | boolean contains(Object obj) |
Tests whether the Vector contains the specified element. |
| 8 | void removeAllElements() |
Removes all elements from the Vector. |
| 9 | void setElementAt(E obj, int index) |
Replaces the element at the specified position with the specified element. |
| 10 | void removeElement(Object obj) |
Removes the first occurrence of the specified element from the Vector. |
Vector inherits all the methods of List, RandomAccess, and Collection interfaces.
Vector class.
import java.util.Vector;
public class VectorDemo
{
public static void main(String[] args)
{
Vector<String> vector = new Vector<>();
// Adding elements
vector.add("Apple");
vector.add("Banana");
vector.add("Mango");
vector.add("Banana"); // duplicate allowed
System.out.println(vector);
System.out.println("-------------------------");
// Accessing first and last elements
System.out.println("First Element: " + vector.firstElement());
System.out.println("Last Element: " + vector.lastElement());
System.out.println("-------------------------");
// Updating element
vector.set(1, "Orange");
System.out.println(vector);
System.out.println("-------------------------");
// Removing element
vector.remove("Apple");
System.out.println(vector);
System.out.println("-------------------------");
// Using Vector as a Stack
vector.add(0, "Grapes"); // equivalent to push at beginning
vector.add("Pineapple"); // add at end
System.out.println(vector);
System.out.println("-------------------------");
// Iterating Vector
for(String fruit : vector)
{
System.out.println(fruit);
}
}
}
[Apple, Banana, Mango, Banana] ------------------------- First Element: Apple Last Element: Banana ------------------------- [Apple, Orange, Mango, Banana] ------------------------- [Orange, Mango, Banana] ------------------------- [Grapes, Orange, Mango, Banana, Pineapple] ------------------------- Grapes Orange Mango Banana Pineapple
Vector is a legacy class in Java and is rarely used nowadays.ArrayList.Vector Class:
index 0.Collections.sort() or list.sort().
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.