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

问题描述 投票:1回答:3

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

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

java for-loop multidimensional-array control-flow
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
...

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]+ " ");
                    }
                }
    }
}

-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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.