List對象遍歷時null判斷


使用for循環遍歷list處理list元素時,對null值判斷:

1、list為null時空指針異常

2、list不為空,但是list.size()=0時,不執行for循環內代碼塊

3、list.size()>0,執行for循環,但循環的對象可能為null
 

  1 package collection;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 public class CollectionTest {
  7 
  8     /**
  9      * @param args
 10      */
 11     public static void main(String[] args) {
 12 
 13         printEachList(1);
 14         printEachList(2);
 15         printEachList(3);
 16         printEachList(4);
 17         
 18         printIterateList(1);
 19         printIterateList(2);
 20         printIterateList(3);
 21         printIterateList(4);
 22         
 23         /*結論:
 24         list為null時空指針異常
 25         list不為空,但是元素為null時,不執行*/
 26         
 27         //標准寫法
 28         {
 29             int key = 2;
 30             List<Long> list = getList(4);
 31             if (null!=list) {//只需要判斷null,size=0時不會執行for循環
 32                 for (Long temp : list) {
 33                     if (null == temp) {//temp可能為null,表示list元素指向的對象為null對象,但是元素的值(null對象的引用)不為空
 34                         continue;
 35                     }
 36                     System.out.println(String.format("key_%d:%s", key,temp.toString()));
 37                 }
 38             }
 39         }
 40         
 41         {
 42             Long a = null;
 43             //非靜態方法來使用一個值為null的引用類型變量,異常
 44             System.out.println(a.getClass());
 45             //靜態方法來使用一個值為null的引用類型變量,正常
 46             System.out.println(Long.getLong("0"));
 47             //靜態方法來使用一個值為null的引用類型變量,正常
 48             System.out.println(a.getLong("0"));
 49         }
 50         
 51     }
 52     
 53     
 54     private  static void printEachList(int key){
 55         List<Long> list = getList(key);
 56         try {
 57             for (Long temp : list) {
 58                 System.out.println(String.format("key_%d:%s", key,temp.toString()));
 59             }
 60         } catch (Exception e) {
 61             System.out.println(String.format("key_%d error:%s", key,e.getMessage()));
 62         }
 63     }
 64     
 65     private  static void printIterateList(int key){
 66         List<Long> list = getList(key);
 67         try {
 68             if (null == list) {
 69                 return;
 70             }
 71             for (int i = 0; i < list.size(); i++) {
 72                 if (null==list.get(i)) {
 73                     continue;
 74                 }
 75                 System.out.println(String.format("key_%d:%s", key,list.get(i).toString()));
 76             }
 77         } catch (Exception e) {
 78             System.out.println(String.format("key_%d error:%s", key,e.getMessage()));
 79         }
 80     }
 81     
 82     private  static List<Long> getList(int key){
 83         
 84         List<Long> list = null;
 85         
 86         switch (key) {
 87         case 1:
 88             list = new ArrayList<Long>();
 89             list.add(1L);
 90             list.add(2L);
 91             break;
 92         case 2:
 93             list = new ArrayList<Long>();
 94             list.add(null);
 95             list.add(1L);
 96             break;
 97         case 3:
 98             list = new ArrayList<Long>();
 99             break;
100         default:
101             break;
102         }
103         
104         return list;
105         
106     }
107 
108 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM