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
Java, null list
提问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 List
instance can't be null
, an instance is always something. A List
type variable can be null
and to test this, use the expression
一个List
实例不能null
,一个实例是永远的东西。甲List
类型变量可以是null
与测试此,使用表达式
List<?> list = null;
if (list == null) {System.out.println("I'm null");}
A List
instance 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 List
instance 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)
{
}