-3

我需要遍历矩阵结构中的每个元素。例如,

SeatingType Model Back  Mech 
1              6   120   58
               7   121   59
               8    

在 java 中,值将以逗号分隔值中的字符串形式作为参数出现,例如 SeatingTpe (1)、Model (6,7,8) 等。

我需要得到结果

1,6,120,58
1,6,120,59
1,6,121,58
1,6,121,59
1,7,120,58
1,7,120,59
1,7,121,58
1,7,121,59
1,8,120,58
1,8,120,59
1,8,121,58
1,8,121,59

请注意,Model、Back 和 Mech 可能为 null。因此,如果 Model 值为 null,则输出应为 1、6、58 和 1、6、59,依此类推。任何帮助请

作为一个开端,我尝试从最后一个元素循环(在本例中为“Mech”)>但这非常乏味。还有其他方法吗?我在这里只提供了 4 个属性。但是reqmnt 是针对11 个属性的。我希望如果我能得到 4 个属性的解决方案,那可以应用于休息 7

4

1 回答 1

1

遍历 4 个循环:

List<String> seatTypeValues = ...
List<String> modelValues = ...
List<String> backValues = ...
List<String> mechValues = ...

if (seatTypeValues.isEmpty()) { seatTypeValues.add(null); }
... // all 4 lists

for(String seatType : seatTypeValues) {
  for(String model : modelValues) {
    for(String back : backValues) {
      for(String mech : mechValues) {
        // print the CSV
        if (seatType != null) {
          writer.write(seatType);
        }
        if (model != null) {
          writer.write(',');
          writer.write(model);
        }
        if (back != null) {
          writer.write(',');
          writer.write(back);
        }
        if (mech != null) {
          writer.write(',');
          writer.write(mech);
        }
        writer.write("\r\n");
      }
    }
  }
}
于 2013-01-14T13:14:59.893 回答