A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 石德志 黑马帝   /  2012-2-25 10:10  /  3032 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 石德志 于 2012-2-25 15:51 编辑

怎么求一个数组的维数?求具体例子代码

评分

参与人数 1技术分 +1 收起 理由
唐秀启 + 1

查看全部评分

8 个回复

倒序浏览
  1. package cn.chdany;

  2. import java.lang.reflect.Array;

  3. public class Dimension {
  4.         public static void main(String[] args) {
  5.                 int[][][][] arr = new int[4][3][2][2];
  6.                 int count=1;
  7.                 Object o =arr[0];
  8.                 while(o.getClass().isArray()){
  9.                         count++;
  10.                         o = Array.get(o, 0);
  11.                 }
  12.                 System.out.println(count);
  13.         }
  14. }
复制代码
回复 使用道具 举报
我不知道有没有具体的函数可以一步解决。。我的方法是每次取出一个元素,看是不是数组,如果是,计数+1,再次判断取出的元素是不是数组

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
我觉得有个方法可以判断,不知道对不对请指教, java每个类都来自于object,在这个类中有个toString()的方法,可以改成这个方法让返回一个数组的维数,这个方法内容是:getClass().getName() + '@' + Integer.toHexString(hashCode()),我们可以提取getClass().getName(),跟具返回"["就可以知道这个几维数组,还可以知道数据的类型,待会测试成功把代码贴上来
回复 使用道具 举报
public static void main(String[] args) {
                    // TODO Auto-generated method stub
                    int y[] = {3};
                    char[] str=y.getClass().getName().toCharArray();
                    getArray(str);
            }
            public static int getArray(char[] str){
                    System.out.println(str.length+":count");
                int count= 0;
                for(int x = 0;x<str.length;x++){
                        if(x<0)
                        {
                                return 0;
                        }else
                        {
                                if(str[x]=='[')
                                {
                                        ++count;
                                }
                        }
                }
                return count;
            }
这个程序有点小问题,有安全问题,传基本类型会有问题,待会再想想处理一下
回复 使用道具 举报
升级版:可以解决上面的错误
public static int getArrayCount(Object...y){       
    System.out.println(y[0].getClass().getSimpleName()+":count");
    char[] str=y[0].getClass().getName().toCharArray();
    int count= 0;
    for(int x = 0;x<str.length;x++){
            if(x<0)
            {
                    return 0;
            }else
            {
                    if(str[x]=='[')
                    {
                            ++count;
                    }
            }
    }
    return count;
}
回复 使用道具 举报
孙汇川 黑马帝 2012-2-25 20:31:42
7#
b.getClass().isArray 可以判断b是不是数组,如果是数组,再可以判断b的第一个元素是不是数组,依次判断。
回复 使用道具 举报
黄秋 黑马帝 2012-2-26 03:31:08
8#
沈样 发表于 2012-2-25 16:04
我觉得有个方法可以判断,不知道对不对请指教, java每个类都来自于object,在这个类中有个toString()的方 ...

你的想法太有创意了! 用toString(),“跟具(根据)返回"["就可以知道这个几维数组”。我原先据此写了个方法dim 得到数组维数,后发现只用一语句也行:
  1. public class Test {
  2.         static int dim(Object obj){
  3.                 int i=0,index=0;
  4.                 while((index=obj.toString().indexOf("[",index))!=-1){
  5.                         i++; index++;
  6.                 }
  7.                 return i;               
  8.         }
  9.         public static void main(String[] args) throws Exception
  10.         {
  11.                 int[] a1 = new int[] { 12, 12, 34 };
  12.                 Integer[] aa = new Integer[] { 12, 12, 34 };
  13.                 Integer[][] a2 = new Integer[][] { { 12, 34 }, { 112, 12, 34 } };
  14.                 int[][][] a5 ={ {{ 12, 34 }}, {{ 112, 12, 34 }} };

  15.                 String[] a3 = new String[] { "asd", "lhm", "zxx", "bxd" };
  16.                 String[][] a4 = new String[][] { { "asd", "lhm", "zxx", "bxd" },{ "asd", "lhm", "zxx", "bxd" } };
  17.                 List[] list ={Arrays.asList(aa),Arrays.asList(a3)};
  18.                
  19.                 System.out.println(dim(a5));
  20.                 System.out.println(a5.toString().split("\\[").length-1); //这样一语句可得维数
  21.    }
  22. }
复制代码
arr.toString().split("\\[").length-1,就得到维数了(对 List[] 也行),够简洁吧,当然效率、兼容性未知,请大家讨论。
回复 使用道具 举报
沈样 黑马帝 2012-2-26 18:41:28
9#
呵呵正则这个视频我还没看到,不会正则下次可以试试呵呵看来要加油了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马