怎样得到数组的维度? 怎样得到数组的维度? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String [][] a = new String[] [] {{"12","13"},{"14","15"},{"16","17"}}; System.out.println(a.length); System.out.println(a[0].length); /** * */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; } } }} /** 获得一个数组的维度(通用方法,比较) */ public static int getDim(Object array) { String s=array.toString(); for(int i=0;i<s.length();i++) { if(s.charAt(i)!='[') return i; } return 0; }可以发现打印数组对象的时候前面都有一个‘[’也恰好是数组的维度,所以可以用以上方法获取数组维度 求解 大侠帮帮忙 java 小小问题 关于文件中重复纪录的处理 如何在成员函数定义时处理异常? 在北京想找一份JAVA程序员的工作难道真的那么难吗? 这段代码有什么问题?维持一个多线程访问的队列Object.wait()和Object.notifyAll() 有关Session和cookie的问题 请教如何获取系统的当前时间 内部类里如何获得外部类的引用??? 那能下到Jbulider的中文帮助? java获取系统进程信息~ JMenuItem 实例能否获取到它的父菜单JMenu 实例的引用,请高手指点。
System.out.println(a.length);
System.out.println(a[0].length);
*
*/
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;
}
}
}
}
public static int getDim(Object array)
{
String s=array.toString();
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)!='[') return i;
}
return 0;
}可以发现打印数组对象的时候前面都有一个‘[’也恰好是数组的维度,所以可以用以上方法获取数组维度