Java 一次从阵列中提取物品的最佳方法10

Java 一次从阵列中提取物品的最佳方法10,java,android,arrays,algorithm,Java,Android,Arrays,Algorithm,我有一个ArrayList,它可以包含无限量的对象。我需要一次提取10个项目并对其进行操作 我能想象的就是这样 int batchAmount = 10; for (int i = 0; i < fullList.size(); i += batchAmount) { List<List<object>> batchList = new ArrayList(); batchList.add(fullList.subList(i, Math.min(i

我有一个ArrayList,它可以包含无限量的对象。我需要一次提取10个项目并对其进行操作

我能想象的就是这样

int batchAmount = 10;
for (int i = 0; i < fullList.size(); i += batchAmount) {
    List<List<object>> batchList = new ArrayList();
    batchList.add(fullList.subList(i, Math.min(i + batchAmount, fullList.size()));
    // Here I can do another for loop in batchList and do operations on each item
}
int batchAmount=10;
对于(int i=0;i

有什么想法吗?谢谢!

从ArrayList中提取元素:
ArrayList。删除(0)

//如果将来需要值,请首先克隆列表:
ArrayList cloned=新的ArrayList(列表);
而(!list.isEmpty()){
object[]tmp=新对象[10];
试一试{
对于(inti=0;i<10;i++)tmp[i]=list.remove(0);
}catch(IndexOutOfBoundsException e){
//已到达列表末尾。for循环已自动断开。无需执行任何操作。
}

//用tmp做点什么,它的.length是有
Google
提供的
Guava库
,方便了不同的功能

List<Integer> countUp = Ints.asList(1, 2, 3, 4, 5);
List<Integer> countDown = Lists.reverse(theList); // {5, 4, 3, 2, 1}

List<List<Integer>> parts = Lists.partition(countUp, 2); // {{1, 2}, {3, 4}, {5}}
List countUp=Ints.asList(1,2,3,4,5);
列表倒计时=列表。反转(列表);/{5,4,3,2,1}
List parts=Lists.partition(countUp,2);//{1,2},{3,4},{5}
答案取自和


希望这能有所帮助。

您可以这样做:

int batchSize = 10;
ArrayList<Integer> batch = new ArrayList<Integer>();
for (int i = 0; i < fullList.size();i++) {
    batch.add(fullList.get(i));
    if (batch.size() % batchSize == 0 || i == (fullList.size()-1)) {
        //ToDo Process the batch;
        batch = new ArrayList<Integer>();
    }
}

你的解决方案还不管用?应该管用,但我想征求其他人对更好的方法的意见。谢谢你对我的实现的理解。我喜欢你的实现。
int batchSize = 10;
ArrayList<Integer> batch = new ArrayList<Integer>();
for (int i = 0; i < fullList.size();i++) {
    batch.add(fullList.get(i));
    if (batch.size() % batchSize == 0 || i == (fullList.size()-1)) {
        //ToDo Process the batch;
        batch = new ArrayList<Integer>();
    }
}
int batchAmount = 10;
List<List<object>> batchList = new ArrayList();
for (int i = 0; i < fullList.size(); i += batchAmount) {
    ArrayList batch = new ArrayList(fullList.subList(i, Math.min(i + batchAmount, fullList.size()));
    batchList.add(batch);
 }
 // at this point, batchList will contain a list of batches