1

我有四个数组,每个数组有 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

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

4

3 回答 3

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
...
于 2012-08-17T19:56:35.317 回答
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]+ " ");
                    }
                }
    }
}
于 2012-08-17T20:25:18.077 回答
-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);
    }
}
于 2012-08-17T19:19:20.033 回答