开发者

Java, null list [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, ov开发者_运维知识库erly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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

thanks


By if condition:

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


List myList = getListFromSomeMethodThatMightReturnNullAlthoughItsBetterToReturnAnEmptyListThenYouWouldntHaveToDoAnyStupidNullChecking();

if (myList == null){

}


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<?> 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:

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

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

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


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

if(myList == null)
{
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜