Java Vector addElement() Method

Java Vector Java Vector

addElement(Object obj)YesVectorA method provided by the class, used toVectoradd an element to the end of the

addElement(Object obj)Adds the specified object as a component to the end of this vector, increasing its size by 1. If the size of the vector exceeds its capacity, its capacity is increased.

Syntax Format

public void addElement(Object obj)

Parameters

  • obj: the element to be added to theVector

Return Value

This method has no return value (void)


Example

Let's understand how to use theaddElement()method through several examples.

Basic Usage

Example

import java.util.Vector;

public class VectorExample {
    public static void main(String[] args) {
        // Create a Vector object
        Vector<String> vector = new Vector<>();
       
        // Use the addElement() method to add elements
        vector.addElement("Apple");
        vector.addElement("Banana");
        vector.addElement("Cherry");
       
        // Print Vector contents
        System.out.println("Vector: " + vector);
    }
}

Output:

Vector: [Apple, Banana, Cherry]

Adding Different Types of Elements

Example

import java.util.Vector;

public class MixedVectorExample {
    public static void main(String[] args) {
        Vector<Object> mixedVector = new Vector<>();
       
        mixedVector.addElement("String");
        mixedVector.addElement(123);      // Autoboxing to Integer
        mixedVector.addElement(45.67);    // Autoboxing to Double
        mixedVector.addElement(true);     // Autoboxing to Boolean
       
        System.out.println("Mixed Vector: " + mixedVector);
    }
}

Output:

<code>Mixed Vector: [String, 123, 45.67, true]
</code>

Comparison with the add() Method

VectorThe class also provides theaddElement()andadd()method, which has very similar functionality, but with some subtle differences:

Feature addElement(Object obj) add(Object obj)
Method Source Vector-specific method From the List interface
Return Value void boolean
Function Add element to the end Add element to the end
Use Case Legacy code Modern code

In fact, in most cases, these two methods can be used interchangeably.addElement()YesVectoris a legacy method of theadd()YesListinterface's standard method.


Notes

  1. Thread Safety:Vectoris thread-safe, but this also means there is additional performance overhead when used in a single-threaded environment.

  2. Capacity Growth: when theVectorneeds to expand, its capacity doubles by default. This differs fromArrayList(ArrayListgrows by 50%).

  3. Generics Support: Although generics are used in the examples, theaddElement()method itself was introduced in Java 1.2, predating generics (Java 5). In non-generic code, it can accept anyObjecttype.

  4. Null Value Handling:addElement()can acceptnullvalues as parameters.


Practical Use Cases

VectorandaddElement()The method may be useful in the following scenarios:

  1. Legacy System Maintenance: When maintaining code written using earlier versions of Java.

  2. Thread Safety Requirements: When you need a simple thread-safe collection without wanting to handle synchronization yourself.

  3. Dynamic Array Requirements: When you need an array that can dynamically resize.

However, in modern Java development, it is generally recommended to useArrayListcombined withCollections.synchronizedList()to achieve similar functionality, as it is more flexible and has better performance.


Performance Considerations

AlthoughVectoris thread-safe, this synchronization incurs performance overhead. In scenarios where thread safety is not needed, usingArrayListwill yield better performance.

addElement()The average time complexity of the method is O(1), but O(n) when expansion is needed, because all elements must be copied to a new array.


Summary

VectorofaddElement(Object obj)The method is a simple but practical method used toVectoradd elements to the end of a collection. Although it may not be commonly used in modern Java development, understanding this method is still valuable for maintaining legacy code and understanding the evolution of the Java collections framework.

Remember, in most new projects, using theArrayListandadd()method may be a better choice, unless you truly need theVectorthread-safety features provided.

Java Vector Java Vector

More Extensions