如何将数组列表拆分成相等的部分? [英] How to split array list into equal parts?
问题描述
有没有将ArrayList拆分成不同的部分而不知道它的大小直到运行时?我知道有一种叫做的方法:
Is there anyway to split ArrayList into different parts without knowing size of it until runtime? I know there is a method called:
list.subList(a,b);
但我们需要明确提及盯着和结束范围的列表。
我的问题是,我们得到一个包含帐号的arraylist,其中包含2000,4000个帐号的数据(编码时间内不会知道数字),我需要将此符号传递给PL的IN查询/ SQL,因为IN不支持超过1000个值,我试图拆分成多个块并将其发送到查询
but we need to explicitly mention staring and ending range of list. My problem is, we get a arraylist containing account numbers which is having data like 2000,4000 account numbers (there numbers will not be known during coding time), and I need to pass this acc nos into IN query of PL/SQL, as IN doesn't support more than 1000 values in it, I am trying to split into multiple chunks and sending it to query
注意:我不能使用任何像Guava等外部库.. :(
这方面的任何指南都很受欢迎。
Note: I cannot use any external libraries like Guava etc.. :( Any guide in this regard is appreciated.
推荐答案
这应该会给你所有部分:
This should give you all your parts :
int partitionSize = 1000;
List<List<Integer>> partitions = new LinkedList<List<Integer>>();
for (int i = 0; i < originalList.size(); i += partitionSize) {
partitions.add(originalList.subList(i,
Math.min(i + partitionSize, originalList.size())));
}
这篇关于如何将数组列表拆分成相等的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!