packagecom.shopping.test;import java.util.*;importjava.awt.List;importjava.lang.reflect.Array;importjava.lang.reflect.Method;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Hashtable;importjava.util.LinkedHashMap;importjava.util.LinkedHashSet;importjava.util.LinkedList;importjava.util.Map;importjava.util.Set;importjava.util.TreeSet;importjava.util.Vector;importjava.util.WeakHashMap;importjava.util.regex.Pattern;importjava.util.Iterator;public classTest {public static void main(String args[]) throwsException {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= newList();
System.out.println("List= " +isNull(aa));
ArrayList aaa= newArrayList();
System.out.println("ArrayList= " +isNull(aaa));
Map map= newHashMap();
System.out.println("HashMap= " +isNull(map));
String a2[][][][]= new String[10][10][10][20];
System.out.println("String a2 [][][][]= " +isNull(a2));
HashMap map2= newHashMap();
System.out.println("HashMap= " +isNull(map2));
Vector keys= newVector();
System.out.println("Vector= " +isNull(keys));
Hashtable ht= newHashtable();
System.out.println("Hashtable= " +isNull(ht));
LinkedList lt= newLinkedList();
System.out.println("LinkedList= " +isNull(lt));
TreeSet tt= newTreeSet();
System.out.println("TreeSet= " +isNull(tt));
Set ss= newTreeSet();
System.out.println("TreeSet= " +isNull(ss));
Iterator it= newArrayList().iterator();
System.out.println("Iterator= " +isNull(it));
LinkedHashMap llp= newLinkedHashMap();
System.out.println("LinkedHashMap= " +isNull(llp));
LinkedHashSet llt= newLinkedHashSet();
System.out.println("LinkedHashSet= " +isNull(llt));
WeakHashMap wp= newWeakHashMap();
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]));
}/***
* 空值检查
*@parampInput 要检查的字符串
*@returnboolean 返回检查结果,但传入的字符串为空的场合,返回真
**/
public static booleanisNull(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;
}
}
}
}
}
}
}