简体   繁体   English

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

[英]is there any function or suggestion to split list into sublist

I'm trying to solve the following problem我正在尝试解决以下问题

I have 3 lists我有 3 个清单

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

and limit is 4 (configurable) then the output should be such that the lists are splitted to not have more than 4 elements put together the list elements are put in sub list based on their list number with limit of 4 elements.限制为 4(可配置),则 output 应将列表拆分为不超过 4 个元素放在一起列表元素根据其列表编号放入子列表中,限制为 4 个元素。

output for above example: output 用于上述示例:

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

I have tried below code to split the list我试过下面的代码来拆分列表

I am getting the output [1, 2, 3, 4][][] I cant move forward for the next list.我得到的是 output [1, 2, 3, 4][][] 我无法继续下一个列表。 Does anyone have suggestions.有没有人有建议。 I know i have very bad programming skill.我知道我的编程技能很差。 Please help.请帮忙。

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);
}

Try this.尝试这个。

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);
    }
}

and

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);

output output

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

If you have four lists and batch size is 6.如果您有四个列表并且批量大小为 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);

output output

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM