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

Java ArrayList isEmpty() Method

Java ArrayList isEmpty() Method

Java ArrayList Java ArrayList

The isEmpty() method is used to determine whether the dynamic array is empty.

The syntax of the isEmpty() method is:


arraylist.isEmpty()

**Note:** arraylist is an object of the ArrayList class.

Parameter Description:

Return Value

If the array contains no elements, returns true.

If the array contains elements, returns false.

Example

Determining whether the dynamic array is empty:

Example

import

java.util.ArrayList

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a dynamic array

        ArrayList

<

String

sites

=

new

ArrayList

<>

(

)

;

       

       

// Check whether the array contains elements

       

boolean

result

=

sites.

isEmpty

(

)

;

// true

       

System

.

out

.

println

(

"Is sites empty? "

result

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

       

System

.

out

.

println

(

"Website list: "

sites

)

;

       

// Check whether this array is empty

        result

=

sites.

isEmpty

(

)

;

 

// false

       

System

.

out

.

println

(

"Is sites empty? "

result

)

;

   

}

}

The output of the above program is:


sites 是否为空? true
网站列表: [Google, Runoob, Taobao]
sites 是否为空? false

In the example above, we created an array named sites; the sites array uses the isEmpty() method to check whether the array contains elements.

Initially, the newly created array contains no elements. Therefore, isEmpty() returns true. After we added some elements (Google, Runoob, etc.), the method returns false.

Java ArrayList Java ArrayList