Java Intermediate

0% completed

Previous
Next
Basic Overview of Vector Class

The Vector class in Java is a dynamic array that implements the List interface. Like ArrayList, it provides methods for storing, accessing, and manipulating a sequence of elements. However, unlike ArrayList, Vector is synchronized by default, which means it is thread-safe and can be used in multithreaded environments where multiple threads access a single instance concurrently.

Image

Vector also extends AbstractList and implements additional interfaces such as List, RandomAccess, Cloneable, and Serializable. This makes it a fully functional and versatile collection class in the Java Collections Framework.

Key Characteristics

  • Implements List Interface: Vector implements the List interface, so it supports all standard list operations, including positional access, insertion, removal, and searching.
  • Dynamic Resizing: Like other dynamic arrays, Vector automatically adjusts its size as elements are added or removed.
  • Synchronized: All methods in Vector are synchronized, providing built-in thread safety, which is useful in concurrent applications.
  • Legacy Class: Although Vector is part of the Java Collections Framework, it is considered legacy. Modern applications often prefer ArrayList when synchronization is not required, or use explicit synchronization if needed.

Syntax for Using Vector

Vector<Type> vector = new Vector<Type>();
  • Explanation:
    • Vector<Type> declares a vector that stores objects of the specified Type.
    • The constructor new Vector<Type>() initializes an empty vector that will grow dynamically as elements are added.

Example: Basic Operations with Vector

In this example, we create a Vector of strings, add several elements, and then access and display those elements. This demonstrates the basic usage of the Vector class while highlighting that it implements the List interface.

Java
Java

. . . .

Example Explanation:

  • Vector Creation:
    • A Vector<String> named fruits is created, which can store string elements.
  • Adding Elements:
    • The add() method is used to insert elements into the vector.
  • Accessing Elements:
    • The get(0) method retrieves the first element, showing how the vector supports index-based access as defined by the List interface.
  • Updating Elements:
    • The set(1, "Blueberry") method replaces the element at index 1, demonstrating the ability to update elements.
  • Removing Elements:
    • The remove(2) method removes the element at index 2.
  • Output:
    • The final output prints the modified vector and the previously accessed first element.

The Vector class is a dynamic array that implements the List interface and offers thread-safe operations through built-in synchronization. Although considered a legacy class, it provides all standard list operations along with additional features like dynamic resizing and thread safety. In this lesson, we reviewed how to create a Vector, add elements, access and update elements by index, and remove elements. Understanding Vector is important for working with legacy code and for scenarios where synchronization is required.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next