第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查 List 對象是否為空或未

檢查 List 對象是否為空或未

慕哥6287543 2022-08-03 16:43:37
我有一個域類,其中域類變量為空,返回1個對象。任何人都可以幫我驗證 . 似乎不起作用。ListListListnullisEmpty法典public static void main(String[] args) {    DomainClass1 d = new DomainClass1();    List<DomainClass1> domain = new ArrayList<>();    d.setTest1(null);    d.setTest2(null);    d.setTest3(null);    d.setTest4(null);    domain.add(d);    System.out.println(domain);    if (domain.isEmpty()) {        System.out.println("is empty");    }}
查看完整描述

2 回答

?
qq_笑_17

TA貢獻1818條經(jīng)驗 獲得超7個贊

null與空不同。列表的大小為 1,這意味著有 1 個對象(即使它的所有屬性都為 null)。如果要檢查其所有屬性是否為空,則可以獲取每個屬性并檢查它是否為空。


您可以使用其他方法來確定屬性是否為 not 的 null,然后只調(diào)用它。例如:


public class DomainClass1 {

   String str1, str2;


   public boolean isEmpty() {

      if (this.str1 != null && this.str2 != null) {

         return false;

      } else {

         return true;

      }

   }

}

現(xiàn)在調(diào)用它,如下所示:


for (int i = 0; i < domain.size(); i++) {

   if (domain.get(i).isEmpty()) {

      // all fields null

   } else {

      // not all are null

   }

}


查看完整回答
反對 回復(fù) 2022-08-03
?
慕尼黑的夜晚無繁華

TA貢獻1864條經(jīng)驗 獲得超6個贊

domain.isEmpty() 

它只是驗證列表是空的,它不驗證d的字段是空的,你可以在DomainClass1類中添加一個方法,就像這樣


public class DomainClass1 {

    private String test1;

    private String test2;

    private String test3;

    private String test4;

    ...

    public boolean isEmpty() {

         if(test1 != null || test2 != null || test3 != null || test4 != null) {

             return false;

         }

         return true;

    }

}

那么你可以使用 d.isEmpty() 來替換 domain.isEmpty()


查看完整回答
反對 回復(fù) 2022-08-03
  • 2 回答
  • 0 關(guān)注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號