Java Vector size() Method
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
- Return Type:
int - Return value: The number of elements in the current
VectorVector - Time complexity: O(1) (constant time)
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
size(): The number of elements currently storedcapacity(): The maximum number of elements the Vector can currently store (without expansion)
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
-
Thread Safety: Although
Vectoris thread-safe, butsize()The value returned by the method may become invalid immediately in a multithreaded environment (if another thread is modifyingVector). -
Performance considerations: For scenarios requiring frequent insertion and deletion operations,
ArrayList: For frequent lookup operations,Vectorperforms better becauseVector's synchronization brings extra overhead. -
Alternatives: In Java 5 and later versions, you can consider using
Collections.synchronizedList()to wrapArrayListto 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:
- Understand how many elements the collection currently contains
- Control loops and iteration
- Check the collection state
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