Android:for-loop / Array挑战

时间:2014-04-23 11:34:59

标签: android multidimensional-array nested-loops

我们有一个包含四个项目的数组,每个项目也是一维数组。

 BIG_Array[ arr1, arr2, arr3, arr4] 
 arr1[...]    arr2[...]   arr3[...]   arr4[...]

现在我想像这样打印孩子BiG_Array的每个项目:

 for(int i=0; i<5; i++)
     {
         i+1 + "Input of Chid array Are:"
            print 1st item of arr1
            print 1st item of arr2
            print 1st item of arr3
            print 1st item of arr4
      }

我只需要编写并说明所有4个数组的第一个条目就是这个,然后所有数组的第二个条目就是等等。

我知道这个问题会解决2d-Dimensional for-loop,但我只需要帮助。

3 个答案:

答案 0 :(得分:0)

我认为你需要这个

ArrayList<ArrayList<String>> arr = new ArrayList<ArrayList<String>>();

    for (int i = 0; i < arr.size(); i++) {
        for (int j = 0; j < arr.get(i).size(); j++) {
            Log.d("DEBUG", j + "th item of "+i+" array = " +arr.get(i).get(j));
        }
    }

答案 1 :(得分:0)

        String[] ar1 = {"a","b","c","d","e","f"};
        String[] ar2 = {"g","h","i","j","k","l"};
        String[] ar3 = {"m","n","o","p","q","r"};
        String[] ar4 = {"s","t","u","v","w","x"};
        String[] ar5 = {"y","z","1","2","3","4"};
        String[][] array = new String[][] { ar1, ar2, ar3, ar4, ar5 };

        for (int i = 0; i < array.length; i++)
        {  
            for (int j = 0; j < array[i].length; i++)
            {  
                System.out.println("i :: "+i+" J :: "+j);
                String p1 = array[i][j]; 
                System.out.println("Data :: "+p1);
            }  
        }  

输出:

i :: 0 J :: 0
Data :: a          // 1st item of array1
i :: 1 J :: 0
Data :: g          // 1st item of array2
i :: 2 J :: 0
Data :: m         // 1st item of array3
i :: 3 J :: 0
Data :: s         // 1st item of array4
i :: 4 J :: 0
Data :: y         // 1st item of array5

答案 2 :(得分:0)

String[] arr1={"1","2"};
 String[] arr2={"3","4"};

    String[][] BIG_Array= {arr1,arr2}; 

for(int i=0; i<BIG_Array.length; i++)
        {
            for (int j=0; j<BIG_Array[i].length; j++) {
            System.out.println(BIG_Array[i][j]);
             }
        }