将ArrayList拆分为给定数量的子ArrayList

时间:2015-11-04 07:07:45

标签: java arrays arraylist

有一个规模为14的Arraylist,我必须把它分成6个子arraylists。

List<Integer> list;
list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14));

例如如下:

14/6 = 2.333

现在当我拿ceil值3时,只会创建5个sub-arraylist,当我拿地板值时,即2,那么将创建7个sub-arraylist 但我需要6

如何实现这一目标?

5 个答案:

答案 0 :(得分:2)

像这样的东西,可能有一些问题,但可以调整

 public static void main(String[] args) {
        List list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                11, 12, 13, 14));
        int number = 6;
        int factor = list.size() / number;

        int fromIndex = 0;
        int toIndex = factor;

        for (int i = 0; i < number; i++) {
            if (i < number - 1) {
                System.out.println(list.subList(fromIndex, toIndex));
                fromIndex = toIndex;
                toIndex = fromIndex + factor;
            } else {
                System.out.println(list.subList(fromIndex, list.size()));
            }
        }
    }

输入 - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12, 13, 14

输出

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

输入 - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12, 13

输出

[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]
[11, 12, 13]

答案 1 :(得分:0)

我建议使用“分而治之”方法,使用递归调用将数组分成两半,直到获得所需的数组数。当然,如果你想要一个奇数个数组,那就意味着你有一个数量是其余数组的两倍。

另一种解决方案是创建6个空列表,只需在从原始列表中删除时向每个列表添加一个项目,直到原始列表用完为止。

答案 2 :(得分:0)

你可以做类似下面的代码

public static void main(String[] args) {
    List<String> arrayList = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14));
    int noofsublist = 6;   //no of subparts
    int chopsize = Math.floor(arrayList.size()/noofsublist);

    for (int start = 0; start < arrayList.size(); start += chopsize) {
        int end = Math.min(start + chopsize, arrayList.size());
        List<String> sublist = arrayList.subList(start, end);
        System.out.println(sublist);
    }
}

答案 3 :(得分:0)

我想这是一个家庭作业问题,但如果这是一个真实的场景,我会使用番石榴的Lists.partition()方法:

List<List<Integer>> partitions = Lists.partition(yourList, n);

其中n是子列表的最大大小。 n的简单公式是yourList.size() / desiredNumberOfSubLists() + 1

在您的情况下,它将是14 / 6 + 1 = 3,但不幸的是,它会创建5个子列表,而不是6个。

所以这里是一个自定义方法,它分成n个分区,尽可能公平地将元素分配给各个列表:

public static <T> List<List<T>> partitionIntoNLists(final List<T> list, final int n) {
    final List<List<T>> listOfLists = new ArrayList<>(n);
    final int[] partitionSizes = new int[n];
    int offset = 0;

    // round robin to distribute partition sizes
    for (int i = 0; i < list.size(); i++) {
        partitionSizes[offset++]++;
        if (offset == n) {
            offset = 0;
        }
    }

    offset = 0;
    for (final int partitionSize : partitionSizes) {
        listOfLists.add(list.subList(offset, offset + partitionSize));
        offset += partitionSize;
    }

    return listOfLists;
}

测试代码:

public static void main(final String[] args) {
    final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
    for (List<Integer> integers : partitionIntoNLists(list, 6)) {
        System.out.println(integers);
    }

}

输出:

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

答案 4 :(得分:0)

List<String> list = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14));
int parts = 6;

for (int i = 0; i < parts; i++) {
    List<String> part = list.subList(i * list.size() / parts,
                                     (i + 1) * list.size() / parts);
    System.out.println(part);
}

输出

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