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

Java Vector size() Method

Java Vector size() Method

Java Vector Java Vector


Vector The class's size() method is used to obtain the number of elements currently contained in the Vector . This is a very basic but important method, often used in collection operations.

Syntax Description


public int size()

Method Characteristics


Usage examples

Basic Usage

Example

import

java.util.Vector

;

public

class

VectorSizeExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a Vector

        Vector

<
String
fruits
=
new
Vector
<>
(
)

;

       

       

// Add elements

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Orange"

)

;

       

       

// Use the size() method to get the number of elements

       

int

size

=

fruits.

size

(

)

;

       

System

.

out

.

println

(

"Number of elements in the Vector: "

size

)

;

   

}

}

Output:

Vector 中的元素数量: 3

Difference from Capacity

Beginners often confuse size() and capacity() method in the component:

Example

Vector

<
Integer
numbers
=
new
Vector
<>
(
10
)

;

// Initial capacity is 10

numbers.

add

(

1

)

;

numbers.

add

(

2

)

;

System

.

out

.

println

(

"Size: "

numbers.

size

(

)

)

;

   

// Output: 2

System

.

out

.

println

(

"Capacity: "

numbers.

capacity

(

)

)

;

// Output: 10


Common Application Scenarios

1. Iterating over a Vector

Example

for

(

int

i

=

0

;

i

<

vector.

size

(

)

;

i

++

)

{

   

System

.

out

.

println

(

vector.

get

(

i

)

)

;

}

2. Checking Whether the Vector Is Empty

Although you can use isEmpty() method, but you can also do this:

Example

if

(

vector.

size

(

)

==

0

)

{

   

System

.

out

.

println

(

"The Vector is empty"

)

;

}

3. Controlling the Number of Loop Iterations

Example

while

(

vector.

size

(

)

<
10
)
{

    vector.

add

(

"Item "

(

vector.

size

(

)

1

)

)

;

}


Notes

  1. Thread Safety: Although Vector is thread-safe, but size() The value returned by the method may become invalid immediately in a multithreaded environment (if another thread is modifying Vector).

  2. Performance considerations: For scenarios requiring frequent insertion and deletion operations,ArrayList : For frequent lookup operations, Vector performs better because Vector 's synchronization brings extra overhead.

  3. Alternatives: In Java 5 and later versions, you can consider using Collections.synchronizedList() to wrap ArrayList to obtain a thread-safe list:


List syncList = Collections.synchronizedList(new ArrayList());

Summary

Vector 's size() method is a simple but very important method that helps us:

Although Vector Its frequency of use has declined in modern Java development (replaced by ArrayList ), but it is still a reliable choice in scenarios requiring thread-safe collections. Understanding the size() method is crucial for mastering the basic operations of the Java collections framework.

Java Vector Java Vector