java 判断对象是否为空或者传入集合对象的是否为空,以及判断数组的长度

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

  1. package Test;  
  2.   
  3. import java.awt.List;  
  4. import java.lang.reflect.Array;  
  5. import java.lang.reflect.Method;  
  6. import java.util.ArrayList;  
  7. import java.util.HashMap;  
  8. import java.util.Hashtable;  
  9. import java.util.LinkedHashMap;  
  10. import java.util.LinkedHashSet;  
  11. import java.util.LinkedList;  
  12. import java.util.Map;  
  13. import java.util.Set;  
  14. import java.util.SortedMap;  
  15. import java.util.TreeMap;  
  16. import java.util.TreeSet;  
  17. import java.util.Vector;  
  18. import java.util.WeakHashMap;  
  19. import java.util.regex.Pattern;  
  20. import java.util.Iterator;   
  21.   
  22. public class TestNull {  
  23.     @SuppressWarnings("unchecked")  
  24.       
  25.     public static void main(String args[]) throws Exception{  
  26.         char ch [][] = new char[10][10];  
  27.         System.out.println("char ch[]= " + isNull(ch));  
  28.           
  29.         byte be [] = new byte[10];  
  30.         System.out.println("byte be[]= " + isNull(be));  
  31.           
  32.         float[] ft = new float[10];  
  33.         System.out.println("float ft[]= " + isNull(ft));  
  34.           
  35.         double ad[] = new double[10];  
  36.         System.out.println("double ad[]= " + isNull(ad));  
  37.           
  38.         int ai[][][] = new int [10][10][10];  
  39.         System.out.println("int ai[]= " + isNull(ai));  
  40.           
  41.         Object ob = null;  
  42.         System.out.println("Object= " + isNull(ob));  
  43.           
  44.         String a [] =null;  
  45.         System.out.println("String a []= " + isNull(a));  
  46.           
  47.         List aa = new List();  
  48.         System.out.println("List= " + isNull(aa));  
  49.           
  50.         ArrayList aaa = new ArrayList();  
  51.         System.out.println("ArrayList= " + isNull(aaa));  
  52.           
  53.         Map map = new HashMap();   
  54.         System.out.println("HashMap= " + isNull(map));  
  55.           
  56.         String a2 [][][][] = new String[10][10][10][20];  
  57.         System.out.println("String a2 [][][][]= " + isNull(a2));  
  58.           
  59.         HashMap map2 = new HashMap();   
  60.         System.out.println("HashMap= " + isNull(map2));  
  61.           
  62.         Vector keys = new Vector();  
  63.         System.out.println("Vector= " + isNull(keys));  
  64.           
  65.         Hashtable ht = new Hashtable();  
  66.         System.out.println("Hashtable= " + isNull(ht));  
  67.           
  68.         LinkedList lt = new LinkedList();  
  69.         System.out.println("LinkedList= " + isNull(lt));  
  70.           
  71.         TreeSet tt = new TreeSet();  
  72.         System.out.println("TreeSet= " + isNull(tt));  
  73.           
  74.         Set ss = new TreeSet();  
  75.         System.out.println("TreeSet= " + isNull(ss));  
  76.           
  77.         Iterator it = new ArrayList().iterator();   
  78.         System.out.println("Iterator= " + isNull(it));  
  79.           
  80.         LinkedHashMap llp = new LinkedHashMap();  
  81.         System.out.println("LinkedHashMap= " + isNull(llp));  
  82.           
  83.         LinkedHashSet llt = new LinkedHashSet();  
  84.         System.out.println("LinkedHashSet= " + isNull(llt));  
  85.           
  86.         WeakHashMap wp =new WeakHashMap();  
  87.         System.out.println("WeakHashMap= " + isNull(wp));  
  88.           
  89.         String sra = "'',a,b,c";  
  90.         System.out.println(sra.split(",")[0]);  
  91.         System.out.println("sra= " + isNull(sra.split(",")[0]));  
  92.           
  93.         SortedMap m= new TreeMap();  
  94.         System.out.println("SortedMap= " + isNull(m));  
  95.     }  
  96.       
  97.     /** 
  98.      * 空值检查<br> 
  99.      * <br> 
  100.      * @param pInput   要检查的字符串<br> 
  101.      * @return boolean 返回检查结果,但传入的字符串为空的场合,返回真<br> 
  102.      */  
  103.     public static boolean isNull (Object pInput) {  
  104.         // 判断参数是否为空或者''  
  105.         if (pInput == null || "".equals(pInput)) {  
  106.             return true;  
  107.         } else if ("java.lang.String".equals(pInput.getClass().getName())){  
  108.             // 判断传入的参数的String类型的  
  109.   
  110.             // 替换各种空格  
  111.             String tmpInput = Pattern.compile("//r|//n|//u3000")  
  112.                                      .matcher((String)pInput).replaceAll("");  
  113.             // 匹配空  
  114.             return Pattern.compile("^(//s)*$")  
  115.                           .matcher(tmpInput).matches();  
  116.         } else {  
  117.             // 方法类  
  118.             Method method = null;  
  119.             String newInput = "";  
  120.             try {  
  121.                 // 访问传入参数的size方法  
  122.                 method = pInput.getClass().getMethod("size");  
  123.                 // 判断size大小  
  124.   
  125.                 // 转换为String类型  
  126.                 newInput = String.valueOf(method.invoke(pInput));  
  127.                 // size为0的场合  
  128.                 if (Integer.parseInt(newInput) == 0) {  
  129.   
  130.                     return true;  
  131.                 } else {  
  132.   
  133.                     return false;  
  134.                 }  
  135.             } catch (Exception e) {  
  136.                 // 访问失败  
  137.                 try {  
  138.                     // 访问传入参数的getItemCount方法  
  139.                     method = pInput.getClass().getMethod("getItemCount");  
  140.                     // 判断size大小  
  141.                       
  142.                     // 转换为String类型  
  143.                     newInput = String.valueOf(method.invoke(pInput));  
  144.                       
  145.                     // getItemCount为0的场合  
  146.                     if (Integer.parseInt(newInput) == 0) {  
  147.   
  148.                         return true;  
  149.                     } else {  
  150.   
  151.                         return false;  
  152.                     }  
  153.                 } catch (Exception ex) {  
  154.                     // 访问失败  
  155.                     try{  
  156.                         // 判断传入参数的长度  
  157.   
  158.                         // 长度为0的场合  
  159.                         if (Array.getLength(pInput) == 0) {  
  160.   
  161.                             return true;  
  162.                         } else {  
  163.   
  164.                             return false;  
  165.                         }  
  166.                     } catch (Exception exx) {  
  167.                         // 访问失败  
  168.                         try{  
  169.                             // 访问传入参数的hasNext方法  
  170.                             method = Iterator.class.getMethod("hasNext");  
  171.                             // 转换String类型  
  172.                             newInput = String.valueOf(method.invoke(pInput));  
  173.                               
  174.                             // 转换hasNext的值  
  175.                             if (!Boolean.valueOf(newInput)) {  
  176.                                 return true;  
  177.                             } else {  
  178.                                 return false;  
  179.                             }  
  180.                               
  181.                         } catch (Exception exxx) {  
  182.                             // 以上场合不满足  
  183.                               
  184.                             return false;  
  185.                         }  
  186.                     }  
  187.                 }  
  188.             }  
  189.         }  
  190.     }  
  191. }  
package Test;import java.awt.List;import java.lang.reflect.Array;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashMap;import java.util.Hashtable;import java.util.LinkedHashMap;import java.util.LinkedHashSet;import java.util.LinkedList;import java.util.Map;import java.util.Set;import java.util.SortedMap;import java.util.TreeMap;import java.util.TreeSet;import java.util.Vector;import java.util.WeakHashMap;import java.util.regex.Pattern;import java.util.Iterator; public class TestNull {    @SuppressWarnings("unchecked")        public static void main(String args[]) throws Exception{        char ch [][] = new char[10][10];        System.out.println("char ch[]= " + isNull(ch));                byte be [] = new byte[10];        System.out.println("byte be[]= " + isNull(be));                float[] ft = new float[10];        System.out.println("float ft[]= " + isNull(ft));                double ad[] = new double[10];        System.out.println("double ad[]= " + isNull(ad));                int ai[][][] = new int [10][10][10];        System.out.println("int ai[]= " + isNull(ai));                Object ob = null;        System.out.println("Object= " + isNull(ob));                String a [] =null;        System.out.println("String a []= " + isNull(a));                List aa = new List();        System.out.println("List= " + isNull(aa));                ArrayList aaa = new ArrayList();        System.out.println("ArrayList= " + isNull(aaa));                Map map = new HashMap();         System.out.println("HashMap= " + isNull(map));                String a2 [][][][] = new String[10][10][10][20];        System.out.println("String a2 [][][][]= " + isNull(a2));                HashMap map2 = new HashMap();         System.out.println("HashMap= " + isNull(map2));                Vector keys = new Vector();        System.out.println("Vector= " + isNull(keys));                Hashtable ht = new Hashtable();        System.out.println("Hashtable= " + isNull(ht));                LinkedList lt = new LinkedList();        System.out.println("LinkedList= " + isNull(lt));                TreeSet tt = new TreeSet();        System.out.println("TreeSet= " + isNull(tt));                Set ss = new TreeSet();        System.out.println("TreeSet= " + isNull(ss));                Iterator it = new ArrayList().iterator();         System.out.println("Iterator= " + isNull(it));                LinkedHashMap llp = new LinkedHashMap();        System.out.println("LinkedHashMap= " + isNull(llp));                LinkedHashSet llt = new LinkedHashSet();        System.out.println("LinkedHashSet= " + isNull(llt));                WeakHashMap wp =new WeakHashMap();        System.out.println("WeakHashMap= " + isNull(wp));                String sra = "'',a,b,c";        System.out.println(sra.split(",")[0]);        System.out.println("sra= " + isNull(sra.split(",")[0]));                SortedMap m= new TreeMap();        System.out.println("SortedMap= " + isNull(m));    }        /**     * 空值检查<br>     * <br>     * @param pInput   要检查的字符串<br>     * @return boolean 返回检查结果,但传入的字符串为空的场合,返回真<br>     */    public static boolean isNull (Object pInput) {     // 判断参数是否为空或者''     if (pInput == null || "".equals(pInput)) {      return true;     } else if ("java.lang.String".equals(pInput.getClass().getName())){      // 判断传入的参数的String类型的      // 替换各种空格      String tmpInput = Pattern.compile("//r|//n|//u3000")                               .matcher((String)pInput).replaceAll("");      // 匹配空      return Pattern.compile("^(//s)*$")                    .matcher(tmpInput).matches();     } else {      // 方法类      Method method = null;      String newInput = "";      try {       // 访问传入参数的size方法       method = pInput.getClass().getMethod("size");       // 判断size大小       // 转换为String类型       newInput = String.valueOf(method.invoke(pInput));       // size为0的场合       if (Integer.parseInt(newInput) == 0) {        return true;       } else {        return false;       }      } catch (Exception e) {       // 访问失败       try {        // 访问传入参数的getItemCount方法        method = pInput.getClass().getMethod("getItemCount");        // 判断size大小                // 转换为String类型        newInput = String.valueOf(method.invoke(pInput));                // getItemCount为0的场合        if (Integer.parseInt(newInput) == 0) {         return true;        } else {         return false;        }       } catch (Exception ex) {        // 访问失败        try{         // 判断传入参数的长度         // 长度为0的场合         if (Array.getLength(pInput) == 0) {          return true;         } else {          return false;         }        } catch (Exception exx) {         // 访问失败         try{          // 访问传入参数的hasNext方法          method = Iterator.class.getMethod("hasNext");          // 转换String类型          newInput = String.valueOf(method.invoke(pInput));                    // 转换hasNext的值          if (!Boolean.valueOf(newInput)) {           return true;          } else {           return false;          }                   } catch (Exception exxx) {          // 以上场合不满足                    return false;         }        }       }      }     }    }}

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值