当前位置: 代码迷 >> Java相关 >> 关于JAVA多维数组中的有关问题
  详细解决方案

关于JAVA多维数组中的有关问题

热度:659   发布时间:2013-02-25 21:45:56.0
关于JAVA多维数组中的问题
问题描述:
一共有29个输出结果,这些输出结果都是将数组中的元素直接依次输出的,我想将这些输出格式改为
System.out.println("第"+1+"个元素为"+ar[i][a][b][c][d][e][f]);
  .
  .
  .
  .
System.out.println("第"+29+"个元素为"+ar[i][a][b][c][d][e][f]);
能否实现?为什么……请帮我解答下
Java code
        public static void main(String[] args)     {        int [][][][][][][] ar={{{{{{            {40,10,41,21},            {25,14,10},            {11,44,4,25,63},            {54,354,71,4},            {65},            {6,365,41,255,11,47},            {57,857,7,7478,540,1}        }}}}}};        int g=(                ar[0][0][0][0][0][0].length+                ar[0][0][0][0][0][1].length+                ar[0][0][0][0][0][2].length+                ar[0][0][0][0][0][3].length+                ar[0][0][0][0][0][4].length+                ar[0][0][0][0][0][5].length+                ar[0][0][0][0][0][6].length);        System.out.println("数组中总元素个数为 "+g);        for(int i=0;i<ar.length;i++)        {            for(int a=0;a<ar[i].length;a++)            {                for(int b=0;b<ar[i][a].length;b++)                {                    for(int c=0;c<ar[i][a][b].length;c++)                    {                        for(int d=0;d<ar[i][a][b][c].length;d++)                        {                            for(int e=0;e<ar[i][a][b][c][d].length;e++)                            {                                for(int f=0;f<ar[i][a][b][c][d][e].length;f++)                                {                                    System.out.println(ar[i][a][b][c][d][e][f]);                                }                            }                        }                    }                }            }        }    }


------解决方案--------------------------------------------------------
楼主的可以实现,但是不知道为啥这么设计?有什么特殊含义吗?没事练习for循环?
------解决方案--------------------------------------------------------
这多维数组有什么实际意义么,我最多见过3维的,而且也不需要那么多循环吧。。

Java code
        int [][][][][][][] ar={{{{{{            {40,10,41,21},            {25,14,10},            {11,44,4,25,63},            {54,354,71,4},            {65},            {6,365,41,255,11,47},            {57,857,7,7478,540,1}        }}}}}};                int g = 0;        for (int[] i : ar[0][0][0][0][0]) {            for (int j : i) {                g++;                System.out.println(j);            }        }        System.out.println("=================== ");        System.out.println("数组中总元素个数为 "+g);
------解决方案--------------------------------------------------------
Java code
public class text3 {    public static void main(String[] args) {        {            int[][][][][][][] ar = { { { { { { { 40, 10, 41, 21 },                    { 25, 14, 10 }, { 11, 44, 4, 25, 63 }, { 54, 354, 71, 4 },                    { 65 }, { 6, 365, 41, 255, 11, 47 },                    { 57, 857, 7, 7478, 540, 1 } } } } } } };            int g = (ar[0][0][0][0][0][0].length + ar[0][0][0][0][0][1].length                    + ar[0][0][0][0][0][2].length + ar[0][0][0][0][0][3].length                    + ar[0][0][0][0][0][4].length + ar[0][0][0][0][0][5].length + ar[0][0][0][0][0][6].length);            System.out.println("数组中总元素个数为 " + g);            int o = g;            for (int i = 0; i < ar.length; i++) {                for (int a = 0; a < ar[i].length; a++) {                    for (int b = 0; b < ar[i][a].length; b++) {                        for (int c = 0; c < ar[i][a][b].length; c++) {                            for (int d = 0; d < ar[i][a][b][c].length; d++) {                                for (int e = 0; e < ar[i][a][b][c][d].length; e++) {                                    for (int f = 0; f < ar[i][a][b][c][d][e].length; f++) {                                        g--;                                        System.out.println("第" + (o - g) + "个元素为:"                                                + ar[i][a][b][c][d][e][f]);                                                                            }                                }                            }                        }                    }                }            }        }    }}
  相关解决方案