search

Home  >  Q&A  >  body text

如图,java 在判断的时候,既然不等于null,为什么还要判断size大于0?不等于null肯定大于0呀?

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

} else {

}

黄舟黄舟2766 days ago1433

reply all(11)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:15:53

    List list = new ArrayList();

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

    reply
    0
  • 阿神

    阿神2017-04-18 10:15:53

    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

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:15:53

    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?

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:15:53

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:15:53

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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:15:53

    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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:15:53

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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:15:53

    passengerList.size()>0
    

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

     !passengerList.isEmpty()

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:15:53

    Which object is there but no data

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:15:53

    
    public static boolean isEmpty(Collection coll) {
        return (coll == null || coll.isEmpty());
    }
    

    reply
    0
  • Cancelreply