Home > front end >  How to correct use of List<List<String>>
How to correct use of List<List<String>>

Time:01-18

I have List<List<String>>

    PcpCharacteristic pcpCharacteristic = new PcpCharacteristic();

    PcpCharacteristicValue pcpCharacteristicValue = new PcpCharacteristicValue();

    //  if (allCharacteristics.get(i) != null) {

    for (List<String> list : allCharacteristics) {

      // name of lists Characteristics
      pcpCharacteristic.setCharacteristic(list.get(0));

      for (int j = 1; j < list.size(); j  ) {

        // value from lists of Characteristics
        pcpCharacteristicValue.setValue(String.valueOf(j));
      }
    }

I need to take get (0) from each sublist and put it in one table (This is the name of the sheets). And put the rest of the elements in each subsheet, except for get(0), into the field of another table. Сan you please tell me how to pass in one cycle for each sublist

CodePudding user response:

I suspect you need something similar to:

List<List<String> allCharacteristics = ...  // or method parameter
Table table = ...                           // or method parameter

for (List<String> list : allCharacteristics) {
    var iter = list.iterator();
    var sheet = new Sheet(iter.next());
    while (iter.hasNext()) {
        var subsheet = new SubSheet(iter.next();
        sheet.addSubsheet(subsheet);
    }
}

Please consider this being just some pseudo-code - missing some checks and details - also no idea what the PcpCharacteristic and PcpCharacteristicValue roles are (which is the table, which a sheet or subsheet, what data they store, which methods they offer, ...)

CodePudding user response:

The signature has to be List<? extends List<?>>:

class Test {
  void a(List<List<?>> list) {}
  void b(List<? extends List<?>> list) {}
  void test() {
    List<List<String>> list = new ArrayList<>();
    b(list); // fine
    a(list); // error
  }
}
  • Related