英文:
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 :
- A{1,2,3,4} , B{} , C{}
- A{5} , B{6,7} , C{8}
- A{} , B{} , C{9,10,11,12}
- 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<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);
}
答案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<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
[[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<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
[[1, 2, 3, 4, 5], [6], [], []]
[[], [7], [8, 9, 10, 11, 12], []]
[[], [], [13, 14, 15], [16, 17, 18]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论