如图,java 在判断的时候,既然不等于null,为什么还要判断size大于0?不等于null肯定大于0呀?
黄舟
黄舟 2017-04-18 10:13:53
0
11
1387

if (passengerList!=null&&passengerList.size()>0) {

} else {

}

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(11)
Peter_Zhu
List list = new ArrayList();

list is not null, but list.size() is indeed equal to 0

阿神

To give you an example
list==null means you don’t have a cup
size==0 means you have a cup, but there is no water in the cup
If you want to drink water, of course you must have a cup, and there is water in the cup
So double judgment is required

迷茫

Please try to answer the following questions:

If it’s not equal to null, why can’t it be equal to zero?

What is the difference between null and object?

What is the difference between null and empty list?

How did the much-criticized nullpointer exception come about?

巴扎黑

First determine whether the object is empty. This condition is only true if the object is not empty and the size of the list is greater than 0. If your object = null, then when you get the size, it must be a null pointer.

大家讲道理

plist != null Filter this: List plist = null;
plist.size() > 0 Filter this: List plist = new ArrayList();

洪涛

list==null means that the object has not been instantiated, list.size()>0 means that the list object cannot contain only one piece of data, and null does not contain size(), it is equal to 0. These are two concepts

大家讲道理

One means that the car has not been built, and the other means that there is a car but no cargo has been loaded

左手右手慢动作
passengerList.size()>0

This way of writing is actually not good enough. The recommended way of writing is this:

 !passengerList.isEmpty()
左手右手慢动作

Which object is there but no data

Peter_Zhu

public static boolean isEmpty(Collection coll) {
    return (coll == null || coll.isEmpty());
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!