Java-利用Class与Array判断多维数组
1 public static int getArrayLevel(Object value){ 2 if(value==null) return 0; 3 if(value.getClass().isArray()){ 4 return 1+getArrayLevel(Array.get(value,0)); 5 } 6 return 0; 7 }
1 //多维数组打印(只针对基本可以打印类型,复杂的Object没法打印) 2 public static String printArray(Object array){ 3 if(array==null) return "null"; 4 if(!array.getClass().isArray()){ 5 return ""+array; 6 }else{ 7 int len = Array.getLength(array); 8 StringBuilder result = new StringBuilder(); 9 if(len>0){ 10 result.append("["+printArray(Array.get(array,0))); 11 for (int i=1;i<len;i++){ 12 result.append(","+printArray(Array.get(array,i))); 13 } 14 result.append("]"); 15 } 16 return result.toString(); 17 } 18 }
(´-﹏-`;)毁灭吧,我累了