有什么功能或建议将列表分成子列表

问题描述

我正在尝试解决以下问题

我有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,12}
  4. A {},B {},C {13,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","15");
List<String> d = List.of("16","17","18");
splitBatch(6,c,d);

输出

[[1,4,5],[6],[]]
[[],[7],[8,9,12],15],[16,17,18]]