Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector addElement() Method

Java Vector addElement() Method

Java Vector Java Vector

addElement(Object obj) Is Vector A method provided by the class, used to add an element to the Vector 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

Return Value

This method has no return value (void)


Example

Let's look at a few examples to understand how to use addElement() method.

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 the Vector contents

       

System

.

out

.

println

(

"Vector: "

vector

)

;

   

}

}

Output result:

Vector: [Apple, Banana, Cherry]

Adding Elements of Different Types

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

)

;

     

// Auto-boxed to Integer

        mixedVector.

addElement

(

45.67

)

;

   

// Auto-boxed to Double

        mixedVector.

addElement

(

true

)

;

   

// Auto-boxed to Boolean

       

       

System

.

out

.

println

(

"Mixed Vector: "

mixedVector

)

;

   

}

}

Output result:

Mixed Vector: [String, 123, 45.67, true]


Comparison with the add() method

Vector The class also provides addElement() and add() methods; their functions are very similar but have some subtle differences:

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

In fact, in most cases these two methods can be used interchangeably.addElement() Is Vector legacy method, while add() Is List standard method of the interface.


Notes

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

  2. Capacity Growth: When Vector When expansion is needed, by default its capacity doubles. This differs from ArrayList (whichArrayList grows by 50%).

  3. Generic Support: Although generics are used in the examples, the addElement() method itself was introduced in Java 1.2, before generics (Java 5). In non-generic code, it can accept any Object type.

  4. Null Value Handling:addElement() can accept null value as a parameter.


Practical Application Scenarios

Vector and addElement() The method may be useful in the following scenarios:

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

  2. Thread Safety Requirements: When a simple thread-safe collection is needed without handling synchronization yourself.

  3. Dynamic Array Requirements: When a dynamically resizable array is needed.

However, in modern Java development, it is usually more recommended to use ArrayList together with Collections.synchronizedList() to implement similar functionality because it is more flexible and performs better.


Performance considerations

Although Vector is thread-safe, but this synchronization brings performance overhead. In scenarios where thread safety is not needed, using ArrayList will give better performance.

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


Summary

Vector 's addElement(Object obj) The method is a simple but practical method used to add an element to the Vector collection. Although it may not be used often in modern Java development, understanding this method is still valuable for maintaining old code and understanding the evolution of the Java collections framework.

Remember, in most new projects, using ArrayList and add() may be a better choice unless you really need Vector the thread safety features provided by

Java Vector Java Vector