码迷,mamicode.com
首页 > 编程语言 > 详细

java判断为空的方法

时间:2019-07-22 18:28:30      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:imp   style   ace   OLE   stat   catch   class   out   判断   

package com.shopping.test;

import java.util.*;
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.TreeSet;
import java.util.Vector;
import java.util.WeakHashMap;
import java.util.regex.Pattern;
import java.util.Iterator;

public class Test {

    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]));
    }

    /**
     *  
     *   空值检查<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;
                        }
                    }
                }
            }
        }
    }
}

 

java判断为空的方法

标签:imp   style   ace   OLE   stat   catch   class   out   判断   

原文地址:https://www.cnblogs.com/wyf-love-dch/p/11227354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!