使用嵌套for循环填充多维数组

时间:2012-08-17 19:12:48

标签: java for-loop multidimensional-array flow-control

我有四个数组,每个数组有3个(但可能更多)元素。我试图用每个元素的所有可能组合填充4 x aa.length * bb.length * cc.length * dd.length数组。我试图用嵌套for循环来做这个,但我的逻辑是错误的。我不确定最有效的方法是什么。 到目前为止,这是我的咖啡因饥饿的大脑所提出的。

String[] AA={DDDD, HHHH, ZZZZ};
String[] BB={DDDD, HHHH, ZZZZ};
String[] CC={DDDD, HHHH, ZZZZ};
String[] DD={DDDD, HHHH, ZZZZ};


String[][] 2Darray = new String[4][AA.length*BB.length*CC.length*DD.length];

for (int i = 0; i <AA.length; i++){

  for (int j = 0; j < BB.length; j++){

    for (int k = 0; k < CC.length; k++){

      for (int L = 0; L < DD.length; L++){

        2Darray[3][i+j+k+L] = DD[L]; 
        2Darray[2][i+j+k] = CC[k];
        2Darray[1][i+j] = BB[j];
        2Darray[0][i] = AA[i];

      }
    }
  }
}

打印输出如下:

DDDD DDDD DDDD DDDD
HHHH DDDD DDDD DDDD
ZZZZ DDDD DDDD DDDD
null HHHH DDDD DDDD
null ZZZZ DDDD DDDD
null null HHHH DDDD
null null ZZZZ DDDD
null null null HHHH
null null null ZZZZ
null null null null
null null null null
null null null null
...etc

有什么更好的方法来解决这个问题?

3 个答案:

答案 0 :(得分:0)

试试这个

    String[] AA = {"DDDD", "HHHH", "ZZZZ"};
    String[] BB = {"DDDD", "HHHH", "ZZZZ"};
    String[] CC = {"DDDD", "HHHH", "ZZZZ"};
    String[] DD = {"DDDD", "HHHH", "ZZZZ"};


    String[][] result = new String[4][AA.length * BB.length * CC.length * DD.length];

    int row = 0;
    for (int i = 0; i < AA.length; i++) {

        for (int j = 0; j < BB.length; j++) {

            for (int k = 0; k < CC.length; k++) {

                for (int L = 0; L < DD.length; L++) {

                    result[3][row] = DD[L];
                    result[2][row] = CC[k];
                    result[1][row] = BB[j];
                    result[0][row] = AA[i];
                    System.out.println(result[0][row] + " " +result[1][row] + " " +result[2][row] + " " +result[3][row]);
                    row++;
                }
            }
        }
    }

和输出

DDDD DDDD DDDD DDDD
DDDD DDDD DDDD HHHH
DDDD DDDD DDDD ZZZZ
DDDD DDDD HHHH DDDD
DDDD DDDD HHHH HHHH
DDDD DDDD HHHH ZZZZ
DDDD DDDD ZZZZ DDDD
...

答案 1 :(得分:0)

public class Demo {
    public static void main(String[] args) {
        String[] AA={"1", "2", "3"};
        String[] BB={"4", "5", "6"};
        String[] CC={"7", "8", "9"};
        String[] DD={"10", "11", "12"};


        String[][] combinations = new String[AA.length*BB.length*CC.length*DD.length][4];

        // STORING INTO 2-DIMENSIONAL ARRAY
        int currentRow = 0;
        for (int i = 0; i <AA.length; i++){
            for (int j = 0; j < BB.length; j++){
                for (int k = 0; k < CC.length; k++){
                    for (int l = 0; l < DD.length; l++){
                        combinations[currentRow][0] = AA[i];
                        combinations[currentRow][1] = BB[j];
                        combinations[currentRow][2] = CC[k];
                        combinations[currentRow][3] = DD[l];
                        currentRow++;
                    }
                }
            }
        }

        // PRINTING THE 2-DIMENSIONAL ARRAY

                for (int i = 0; i < AA.length*BB.length*CC.length*DD.length; i++){
                    System.out.println();
                    for (int j = 0; j < 4; j++){
                        System.out.print(combinations[i][j]+ " ");
                    }
                }
    }
}

答案 2 :(得分:-1)

我相信这就是你想要做的。请记住,多维数组元素只是数组(初始化为null)。此外,您获得了向后维度的顺序。

编辑:您还必须将2Darray更改为其他内容,因为标识符不能以数字开头。 (在这种情况下,我将其更改为x2Darray)。

public class multidim{
    public static void main(String[] args)
    {
        String[] AA={"DDDD", "HHHH", "ZZZZ"};
        String[] BB={"DDDD", "HHHH", "ZZZZ"};
        String[] CC={"DDDD", "HHHH", "ZZZZ"};
        String[] DD={"DDDD", "HHHH", "ZZZZ"};

        String[][] x2Darray = new String[AA.length*BB.length*CC.length*DD.length][4];

        for (int i = 0; i <AA.length; i++){
          for (int j = 0; j < BB.length; j++){
            for (int k = 0; k < CC.length; k++){
              for (int L = 0; L < DD.length; L++){
                String[] temp = {AA[i], BB[j], CC[k], DD[L]};
                x2Darray[((i*BB.length + j)*CC.length + k)*DD.length + L] = temp;
              }
            }
          }
        }

        StringBuilder s = new StringBuilder();
        for(String[] row: x2Darray){
            for(String x: row){ s.append(x); s.append(' '); }
            s.append("\n");             
        }

        System.out.println(s);
    }
}