是否有任何功能或建议将列表拆分为子列表

is there any function or suggestion to split list into sublist

我正在尝试解决以下问题

我有 3 个列表

A {1.2.3.4.5} 
B {6,7}
C {8,9,10,11,12,13,14,15}  

并且限制为 4(可配置) 那么输出应该是这样的,列表被分割成不超过 4 个元素放在一起 列表元素根据其列表编号放入子列表中,限制为 4 个元素。

以上示例的输出:

  1. A{1,2,3,4} , B{} , C{}
  2. A{5}, B{6,7}, C{8}
  3. A{}, B{}, C{9,10,11,12}
  4. A{}, B{}, C{13,14,15}

我试过下面的代码来拆分列表

我正在获取输出 [1, 2, 3, 4][][] 我无法继续下一个列表。有没有人有建议。 我知道我的编程技巧很差。请帮忙

public void listDivider() {
    ArrayList<String> al1 = new ArrayList<String>();
    ArrayList<String> al2 = new ArrayList<String>();
    ArrayList<String> al3 = new ArrayList<String>();
    al1.add("1");
    al1.add("2");
    al1.add("3");
    al1.add("4");
    al1.add("5");
    System.out.println("List 1>" + al1);
    al2.add("6");
    al2.add("7");
    System.out.println("List 2>" + al2);
    al3.add("8");
    al3.add("9");
    al3.add("10");
    al3.add("11");
    al3.add("12");
    al3.add("13");
    al3.add("14");
    al3.add("15");
    float batchSize = 4;
    float totalListElements = Math.round((al1.size() + al2.size() + al3.size()) / batchSize);
    LinkedList<String> allElements = new LinkedList<String>();
    allElements.addAll(al1);
    allElements.add("|");
    allElements.addAll(al2);
    allElements.add("|");
    allElements.addAll(al3);
    ArrayList<String> added1 = new ArrayList<>();
    ArrayList<String> added2 = new ArrayList<>();
    ArrayList<String> added3 = new ArrayList<>();
    int position = 0;
    int count = 0;    
    for (int i = 0; i < allElements.size(); i++) {
        String check = allElements.get(i);
        if (check.equals("|")) {
            position++;    
        }
        if (count <= 4) {
            if (position == 1) {
                if (count == 4) {
                    count = 0;
                    System.out.print(added1);
                    System.out.print(added2);
                    System.out.print(added3);
                    added1.clear();
                    added2.clear();
                    added3.clear();
                    break;
                }  
                added1.add(allElements.removeFirst());
                count++;
            }
        }
    }
    System.out.println("\n\n\n\n"+allElements);
}

试试这个。

static void splitBatch(int batchSize, List<String>...lists) {
    int length = lists.length;
    int[] indexes = new int[length];
    int total = 0;
    for (List<String> list : lists)
        total += list.size();
    int selected = 0;
    List<List<String>> batch = new ArrayList<>();
    while (selected < total) {
        batch.clear();
        int count = batchSize;
        for (int i = 0; i < length; ++i) {
            int index = indexes[i];
            int size = lists[i].size();
            int subSize = Math.min(count, Math.min(batchSize, size - index));
            List<String> sublist = lists[i].subList(index, index + subSize);
            batch.add(sublist);
            indexes[i] += subSize;
            count -= subSize;
            selected += subSize;
        }
        System.out.println(batch);
    }
}

List<String> a = List.of("1", "2", "3", "4", "5");
List<String> b = List.of("6", "7");
List<String> c = List.of("8", "9", "10", "11", "12", "13", "14", "15");
splitBatch(4, a, b, c);

产出

[[1, 2, 3, 4], [], []]
[[5], [6, 7], [8]]
[[], [], [9, 10, 11, 12]]
[[], [], [13, 14, 15]]

如果您有四个列表且批量大小为 6。

List<String> a = List.of("1", "2", "3", "4", "5");
List<String> b = List.of("6", "7");
List<String> c = List.of("8", "9", "10", "11", "12", "13", "14", "15");
List<String> d = List.of("16", "17", "18");
splitBatch(6, a, b, c, d);

产出

[[1, 2, 3, 4, 5], [6], [], []]
[[], [7], [8, 9, 10, 11, 12], []]
[[], [], [13, 14, 15], [16, 17, 18]]