java isempty用法_Java List isEmpty()用法及代码示例

Java中的List接口的isEmpty()方法用于检查列表是否为空。如果列表不包含任何元素,则返回true;否则,如果列表包含任何元素,则返回false。

用法:

boolean isEmpty()

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

返回值:如果列表中没有其他元素,则返回True。返回类型为boolean数据类型。

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

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

// Java code to demonstrate the working of

// isEmpty() method in List interface

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// creating an Empty Integer List

List arr = new ArrayList(10);

// check if the list is empty or not

// using isEmpty() fucntion

boolean ans = arr.isEmpty();

if (ans == true)

System.out.println("The List is empty");

else

System.out.println("The List is not empty");

// addition of a element to

// the List

arr.add(1);

// check if the list is empty or not

// after adding an element

ans = arr.isEmpty();

if (ans == true)

System.out.println("The List is empty");

else

System.out.println("The List is not empty");

}

}

输出:

The List is empty

The List is not empty

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值