Java中的ArrayList isEmpty()及其示例

这个 isEmpty() 方法 ArrayList 在java中,用于检查列表是否为空。如果列表不包含任何元素,则返回true;如果列表包含任何元素,则返回false。

null

语法:

list_name.isEmpty()

参数: 它不接受任何参数。

返回: 如果列表 列出你的名字 没有其他元素,则返回false。返回类型为布尔型数据类型。

错误和例外: 此方法没有错误或异常。

演示isEmpty()在Java中工作的程序:

// Java code to demonstrate the working of
// isEmpty() method in ArrayList
// for ArrayList functions
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
// creating an Empty Integer ArrayList
ArrayList<Integer> arr = new ArrayList<Integer>( 10 );
// check if the list is empty or not using function
boolean ans = arr.isEmpty();
if (ans == true )
System.out.println( "The ArrayList is empty" );
else
System.out.println( "The ArrayList is not empty" );
// addition of a element to the ArrayList
arr.add( 1 );
// check if the list is empty or not
ans = arr.isEmpty();
if (ans == true )
System.out.println( "The ArrayList is empty" );
else
System.out.println( "The ArrayList is not empty" );
}
}


输出:

The ArrayList is empty
The ArrayList is not empty
© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享