有没有将列表拆分为子列表的功能或建议?

huangapple go评论77阅读模式
英文:

is there any function or suggestion to split list into sublist

问题

I've translated the code portion as requested:

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

Please note that this code appears to be an attempt to split three lists into sublists of at most 4 elements each. However, it seems incomplete, and there may be issues with the logic. If you have specific questions or need further assistance with this code, please let me know.

英文:

I'm trying to solve the following problem

I have 3 lists

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.

output for above example :

  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}

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. Does anyone have suggestions.
I know i have very bad programming skill. Please help.

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

答案1

得分: 0

请尝试这个代码:

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]]
英文:

Try this.

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

and

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

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.

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

output

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

huangapple
  • 本文由 发表于 2020年8月11日 05:06:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63348036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定