怎样得到数组的维度? 

解决方案 »

  1.   

    String [][] a = new String[] [] {{"12","13"},{"14","15"},{"16","17"}};
    System.out.println(a.length);
    System.out.println(a[0].length);
      

  2.   

    /**
     * 
     */
    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;
    }
    }
    }
    }
      

  3.   

    /** 获得一个数组的维度(通用方法,比较) */
    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;
    }可以发现打印数组对象的时候前面都有一个‘[’也恰好是数组的维度,所以可以用以上方法获取数组维度