简体   繁体   English

将项目分成3d ArrayList

[英]Separating items into a 3d ArrayList

I have a 2D List of type Integer ( ArrayList< List < Integer > > ), There are currently 177147 lists of integers . 我有一个类型为Integer的2D ListArrayList< List < Integer > > ),当前有177147个整数列表 I want to separate these into 243 different collections (Each collection having 729 lists of elements) 我想将它们分为243个不同的集合(每个集合都有729个元素列表)

eg Array[0] -> Array[728] go into Collection[0] ...until Array[176417] -> Array[177146] goes into Collection[242] 例如Array[0] -> Array[728] go into Collection[0] ...直到Array[176417] -> Array[177146] goes into Collection[242]

Should I rather use ArrayList<ArrayList<List<Integer>>> ? 我应该使用ArrayList<ArrayList<List<Integer>>>吗? If so, how do I separate these items in this manner? 如果是这样,我如何以这种方式分离这些项目?

Iterate on the elements and add by slices to the new structure: 遍历元素,并通过切片将其添加到新结构中:

public void run() {
    int SLICE = 729;
    List<List<Integer>> list2d = new ArrayList<List<Integer>>();
    // fill original list
    List<List<List<Integer>>> list3d = new ArrayList<List<List<Integer>>>();
    List<List<Integer>> partial = new ArrayList<List<Integer>>(SLICE);
    for (List<Integer> list : list2d) {
        partial.add(list);
        if (partial.size() == SLICE) {
            list3d.add(partial);
            partial = new ArrayList<List<Integer>>(SLICE);
        }
    }
    if (!partial.isEmpty()) {
        list3d.add(partial);
    }
}
for(int c = 0; c <= 242; c++)
{
    for(int i = 0; i < 729; i++)
    {
        int position = (c * 729) + i;
        Collection[c][i] = Array[position];
    }
}

You might want to check my maths on the formula for position, but the idea is sound. 您可能想检查一下我关于位置公式的数学知识,但是这个想法很合理。

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

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