Search code examples
javacollections

How to correct use of List<List<String>>


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


Solution

  • 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, ...)