日期:2014-05-20  浏览次数:20790 次

怎样得到数组的维度?
怎样得到数组的维度?  



------解决方案--------------------
String [][] a = new String[] [] {{ "12 ", "13 "},{ "14 ", "15 "},{ "16 ", "17 "}};
System.out.println(a.length);
System.out.println(a[0].length);
------解决方案--------------------
楼上的肯定不对,如果是

{{ "14 "},{ "16 ", "17 "}};

还是二维
------解决方案--------------------
有什么用?
------解决方案--------------------
/**
*
*/
package zhangshu.test.test1;

/**
* @author wdman
*
*/
public class Test1 {

/**
* @param args
*/
public static void main(String[] args) {
String[][] testStr1 = {{ "aa ", "bb "},{ " "},{ " "}};
String[][][][] testStr2 = {{},{{{ "aa "}}},{{}}};
String[][][][][] testStr3 = {{},{{{{}}}},{{}}};
String testStr4 = " ";
String[] testStr5 = {};
System.out.println(cal(testStr1,0));
System.out.println(cal(testStr2,0));
System.out.println(cal(testStr3,0));
System.out.println(cal(testStr4,0));
System.out.println(cal(testStr5,0));
}

private static int cal(Object arrays, int result) {
try {
Object[] arys = (Object[])arrays;

if (arys == null) return result;

if (arys.length == 0) {
return result + 1;
} else {
int index = 0;
int maxX = 0;
for (int i=0; i <arys.length; i++) {
Object[] childArys = (Object[])arys[i];
if (childArys == null){
continue;
} else {
if (childArys.length > maxX) {
maxX = childArys.length;
index = i;
}
}
}
return cal(arys[index],result+1);
}
} catch (ClassCastException cce) {
if (result == 0) {
return 0;
} else {
return result+1;
}
}
}
}

------解决方案--------------------
帮定