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

Java Vector isEmpty() Method

Java Vector isEmpty() Method

Java Vector Java Vector


isEmpty() The method is in Java's Vector A commonly used method provided by the class, used to check whether the vector (Vector) is empty. This method belongs to java.util.Vector class and is part of the Java Collections Framework.

Method Syntax:


public boolean isEmpty()

Method Functionality

isEmpty() The main function of the method is to determine whether the current Vector object contains no elements. This method returns a boolean value:


Usage examples

Below we use several examples to demonstrate isEmpty() how the method is used.

Example 1: Checking an Empty Vector

Example

import

java.util.Vector

;

public

class

VectorExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create an empty vector

        Vector

<

String

vector

=

new

Vector

<>

(

)

;

       

       

// Check whether the vector is empty

       

if

(

vector.

isEmpty

(

)

)

{

           

System

.

out

.

println

(

"The vector is empty"

)

;

       

}

else

{

           

System

.

out

.

println

(

"The vector is not empty"

)

;

       

}

   

}

}

Output result:

向量是空的

Example 2: Checking a Non-Empty Vector

Example

import

java.util.Vector

;

public

class

VectorExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a vector and add elements

        Vector

<

Integer

numbers

=

new

Vector

<>

(

)

;

        numbers.

add

(

10

)

;

        numbers.

add

(

20

)

;

       

       

// Check whether the vector is empty

       

if

(

numbers.

isEmpty

(

)

)

{

           

System

.

out

.

println

(

"The vector is empty"

)

;

       

}

else

{

           

System

.

out

.

println

(

"The vector is not empty; it contains "

numbers.

size

(

)

" elements"

)

;

       

}

   

}

}

Output result:

向量不为空,包含 2 个元素

Method implementation principle

In Java's Vector In the class,isEmpty() The method's implementation is very simple. It just checks whether the vector's elementCount(element count) is 0:

Example

public

boolean

isEmpty

(

)

{

   

return

elementCount

==

0

;

}

where elementCount Is Vector an internal field of the class, used to record the number of elements actually stored in the current vector.


Comparison with Other Methods

isEmpty() vs size() == 0

isEmpty() The method and the size() == 0 expression are functionally equivalent, but it is usually recommended to use isEmpty(), because:

  1. Better readability and clearer semantics
  2. In some collection implementations,isEmpty() may have a more efficient implementation

isEmpty() vs null Check

Note that theisEmpty() method can only be used to check whether an initialized Vector object is empty. If the Vector object itself is null, calling isEmpty() will cause NullPointerException.

Example

Vector

<

String

vector

=

null

;

System

.

out

.

println

(

vector.

isEmpty

(

)

)

;

// Throws NullPointerException

The correct approach is to first check whether it is null, then check whether it is empty:

Example

if

(

vector

==

null

||

vector.

isEmpty

(

)

)

{

   

System

.

out

.

println

(

"The vector is null or empty"

)

;

}


Best Practices

  1. When you need to check whether a collection is empty, prefer isEmpty() instead of size() == 0
  2. When using isEmpty() Before that, ensure the Vector object has been initialized
  3. For null 's Vector objects that may be, first perform a null Checks
  4. Using in a concurrent environment Vector , note synchronization issues

Summary

Vector 's isEmpty() method is a simple but practical utility method that provides a clear way to check whether a vector is empty. Understanding and correctly using this method can help you write more robust and readable code. Remember, when using it in a concurrent environment, Vector , although Vector is thread-safe, you still need to pay attention to the atomicity of compound operations.

Java Vector Java Vector