Java,空列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5421878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:02:24  来源:igfitidea点击:

Java, null list

java

提问by aneuryzm

How can I verify if a List is null in Java ?

如何验证 Java 中的 List 是否为空?

thanks

谢谢

回答by dogbane

By if condition:

通过 if 条件:

if (list == null){
  // do something
}

回答by blank

List myList = getListFromSomeMethodThatMightReturnNullAlthoughItsBetterToReturnAnEmptyListThenYouWouldntHaveToDoAnyStupidNullChecking();

if (myList == null){

}

回答by Andreas Dolk

A Listinstance can't be null, an instance is always something. A Listtype variable can be nulland to test this, use the expression

一个List实例不能null,一个实例是永远的东西。甲List类型变量可以是null与测试此,使用表达式

 List<?> list = null;
 if (list == null) {System.out.println("I'm null");}

A Listinstance can by empty, meaning the list doesn't contain any values. To ways to test this:

一个List实例可以通过,这意味着列表中不包含任何值。测试方法:

 if (list.size() == 0) {...}     
 if (list.isEmpty()) {...}

A Listinstance can contain items that represent null. To find those, iterate through the list:

一个List实例可以包含代表null. 要找到这些,请遍历列表:

 for(Object o:list)
   if (o == null) {...}

回答by Jean-Louis Mbaka

If you have a variable myList of type List, you can do this by:

如果您有一个 myList 类型的变量List,您可以通过以下方式执行此操作:

if(myList == null)
{
}