【问题标题】:How to split an arraylist in multiple arraylists of k lenght in Java ? I don't want a view of the new lists, but I want to create them如何在 Java 中将一个数组列表拆分为多个长度为 k 的数组列表?我不想查看新列表,但我想创建它们
【发布时间】:2020-01-17 18:32:40
【问题描述】:

所以,我已经阅读了很多关于如何从多个数组列表创建 Arraylist 的内容,但并非相反。我发现一些代码可以查看基于单个数组列表的某些数组列表,但我不需要该视图。我需要从一个数组列表(长度为 81)创建新的数组列表列表(长度为 9)。 问题是我需要有可能单独管理每个数组列表。

static ArrayList<Integer> nums = new ArrayList<Integer>();

> Output : 
[0, 0, 1, 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0,
    > 6, 7, 0, 7, 8, 0, 8, 9, 1, 0, 2, 1, 1, 3, 1, 2, 4, 1, 3, 5, 1, 4, 6,
    > 1, 5, 7, 1, 6, 8, 1, 7, 9, 1, 8, 1, 2, 0, 3, 2, 1, 4, 2, 2, 5, 2, 3,
    > 6, 2, 4, 7, 2, 5, 8, 2, 6, 9, 2, 7, 1, 2, 8, 2, 3, 0, 4, 3, 1, 5, 3,
    > 2, 6, 3, 3, 7, 3, 4, 8, 3, 5, 9, 3, 6, 1, 3, 7, 2, 3, 8, 3, 4, 0, 5,
    > 4, 1, 6, 4, 2, 7, 4, 3, 8, 4, 4, 9, 4, 5, 1, 4, 6, 2, 4, 7, 3, 4, 8,
    > 4, 5, 0, 6, 5, 1, 7, 5, 2, 8, 5, 3, 9, 5, 4, 1, 5, 5, 2, 5, 6, 3, 5,
    > 7, 4, 5, 8, 5, 6, 0, 7, 6, 1, 8, 6, 2, 9, 6, 3, 1, 6, 4, 2, 6, 5, 3,
    > 6, 6, 4, 6, 7, 5, 6, 8, 6, 7, 0, 8, 7, 1, 9, 7, 2, 1, 7, 3, 2, 7, 4,
    > 3, 7, 5, 4, 7, 6, 5, 7, 7, 6, 7, 8, 7, 8, 0, 9, 8, 1, 1, 8, 2, 2, 8,
    > 3, 3, 8, 4, 4, 8, 5, 5, 8, 6, 6, 8, 7, 7, 8, 8, 8]

我找到了这段代码:

List<Integer> bigList = nums
List<List<Integer>> smallerLists = Lists.partition(bigList, 10);

但由于某种原因它不起作用,因为我无法在 Guava 库中导入 lists.partition() 方法

【问题讨论】:

  • 您是否考虑过使用循环而不是尝试查找库函数?

标签: java arraylist split


【解决方案1】:

你可以这样做:

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Integer> bigList = List.of(0, 0, 1, 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 7, 0, 7, 8, 0, 8, 9,
                1, 0, 2, 1, 1, 3, 1, 2, 4, 1, 3, 5, 1, 4, 6, 1, 5, 7, 1, 6, 8, 1, 7, 9, 1, 8, 1, 2, 0, 3, 2, 1, 4, 2, 2,
                5, 2, 3, 6, 2, 4, 7, 2, 5, 8, 2, 6, 9, 2, 7, 1, 2, 8, 2, 3, 0, 4, 3, 1, 5, 3, 2, 6, 3, 3, 7, 3, 4, 8, 3,
                5, 9, 3, 6, 1, 3, 7, 2, 3, 8, 3, 4, 0, 5, 4, 1, 6, 4, 2, 7, 4, 3, 8, 4, 4, 9, 4, 5, 1, 4, 6, 2, 4, 7, 3,
                4, 8, 4, 5, 0, 6, 5, 1, 7, 5, 2, 8, 5, 3, 9, 5, 4, 1, 5, 5, 2, 5, 6, 3, 5, 7, 4, 5, 8, 5, 6, 0, 7, 6, 1,
                8, 6, 2, 9, 6, 3, 1, 6, 4, 2, 6, 5, 3, 6, 6, 4, 6, 7, 5, 6, 8, 6, 7, 0, 8, 7, 1, 9, 7, 2, 1, 7, 3, 2, 7,
                4, 3, 7, 5, 4, 7, 6, 5, 7, 7, 6, 7, 8, 7, 8, 0, 9, 8, 1, 1, 8, 2, 2, 8, 3, 3, 8, 4, 4, 8, 5, 5, 8, 6, 6,
                8, 7, 7, 8, 8, 8);

        final AtomicInteger counter = new AtomicInteger(0);
        Collection<List<Integer>> smallerLists = bigList.stream()
                .collect(Collectors.groupingBy(i -> counter.getAndIncrement() / 9)).values();

        // Display the smaller lists
        smallerLists.stream().forEach(System.out::println);
    }
}

输出:

[0, 0, 1, 0, 1, 2, 0, 2, 3]
[0, 3, 4, 0, 4, 5, 0, 5, 6]
[0, 6, 7, 0, 7, 8, 0, 8, 9]
[1, 0, 2, 1, 1, 3, 1, 2, 4]
[1, 3, 5, 1, 4, 6, 1, 5, 7]
[1, 6, 8, 1, 7, 9, 1, 8, 1]
[2, 0, 3, 2, 1, 4, 2, 2, 5]
[2, 3, 6, 2, 4, 7, 2, 5, 8]
[2, 6, 9, 2, 7, 1, 2, 8, 2]
[3, 0, 4, 3, 1, 5, 3, 2, 6]
[3, 3, 7, 3, 4, 8, 3, 5, 9]
[3, 6, 1, 3, 7, 2, 3, 8, 3]
[4, 0, 5, 4, 1, 6, 4, 2, 7]
[4, 3, 8, 4, 4, 9, 4, 5, 1]
[4, 6, 2, 4, 7, 3, 4, 8, 4]
[5, 0, 6, 5, 1, 7, 5, 2, 8]
[5, 3, 9, 5, 4, 1, 5, 5, 2]
[5, 6, 3, 5, 7, 4, 5, 8, 5]
[6, 0, 7, 6, 1, 8, 6, 2, 9]
[6, 3, 1, 6, 4, 2, 6, 5, 3]
[6, 6, 4, 6, 7, 5, 6, 8, 6]
[7, 0, 8, 7, 1, 9, 7, 2, 1]
[7, 3, 2, 7, 4, 3, 7, 5, 4]
[7, 6, 5, 7, 7, 6, 7, 8, 7]
[8, 0, 9, 8, 1, 1, 8, 2, 2]
[8, 3, 3, 8, 4, 4, 8, 5, 5]
[8, 6, 6, 8, 7, 7, 8, 8, 8]

【讨论】:

  • 谢谢,但是每个子列表的名称是什么?我的意思是,如果我只想使用第一个子列表:[0, 0, 1, 0, 1, 2, 0, 2, 3],那么列表的名称是什么?
【解决方案2】:

您可以手动遍历每个元素,如果您的子列表有 9 个元素,则创建一个新元素(您可以将 9 替换为您喜欢的任何数字)。并不是说如果您有许多不能被 9 整除的元素,则 listOfLists 中最后一个子列表的长度将是

import java.util.ArrayList;

public class Test {
    public static void main(String s[]) {  
        int[] data = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };

        ArrayList<ArrayList<Integer>> listOfLists = new ArrayList<>();

        // Initialize to 9, so that we are forced to make a new sub-list initially
        int subListLength = 9;

        for (int i = 0; i < data.length; i++) {
            // If our previous sub list is full, make a new one and reset our subListLength
            if (subListLength == 9) {
                listOfLists.add(new ArrayList<Integer>());
                subListLength = 0;
            }

            // Add the next data element to the last generated sub-list and increment the sub list length
            listOfLists.get(listOfLists.size()-1).add(data[i]);
            subListLength++;
        }

        System.out.println(listOfLists);
    }  
}

【讨论】:

    【解决方案3】:

    我会考虑使用 List.subList 自己进行分区。

    给定一个长度为 X 的数据集,并且期望的分区大小为 Y 将能够制作 X / Y 完整子列表和 X % Y 元素的 1 个子列表 (其中 X % Y != 0)。

    private static List<List<Integer>> partition(List<Integer> data, int partitionListLen) {
        int fullPartitionQty = data.size() / partitionListLen;
    
        List<List<Integer>> partitions = new ArrayList<List<Integer>>();
    
        for (int listIndex = 0; listIndex < fullPartitionQty; listIndex++) {
            int startIndex = listIndex * partitionListLen;
            int endIndex = startIndex + partitionListLen;
            List<Integer> subList = data.subList(startIndex, endIndex);
           // System.out.println(subList);
            partitions.add(subList);
        }
    
        //Make one last list that includes the remainder content at the end of the list (if applicable)
        int remainder = data.size() % partitionListLen; 
        if (remainder != 0 ) {
            List<Integer> remainderContent = data.subList(data.size() - remainder, data.size());
            //System.out.println(remainderContent);
            partitions.add(remainderContent);
        }
        return partitions;
    }
    
    
    public static void main(String[] args) {
        //I use this below to generate data sets of these sizes.
        int[] dataSampleSizes = new int[] {0, 37, 81,82};
    
        //# of elements to put in each sublist.   
        int partitionListLen = 9;
        for (int dataSize : dataSampleSizes) {
            //This is the 'data' element
            List<Integer> genData = new ArrayList<Integer>();
            //Filling the data set with ints up to the requested size
            for (int dataPoint = 0; dataPoint < dataSize; dataPoint ++) {
                genData.add(dataPoint);
            }           
            //partition the generated list into chunk blocks
            List<List<Integer>> result = partition(genData, partitionListLen);
    
            //Courtesy output for validation.  This should be verified via unit test.
            System.out.println(String.format("Partitioning a list of %s elements into sublists of %s elements", dataSize, partitionListLen));           
            System.out.println(result.size() + " sublists created");
            System.out.println(result);
        }
    
    }
    

    输出:

    Partitioning a list of 0 elements into sublists of 9 elements
    0 sublists created
    []
    Partitioning a list of 37 elements into sublists of 9 elements
    5 sublists created
    [[0, 1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, 32, 33, 34, 35], [36]]
    Partitioning a list of 81 elements into sublists of 9 elements
    9 sublists created
    [[0, 1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, 32, 33, 34, 35], [36, 37, 38, 39, 40, 41, 42, 43, 44], [45, 46, 47, 48, 49, 50, 51, 52, 53], [54, 55, 56, 57, 58, 59, 60, 61, 62], [63, 64, 65, 66, 67, 68, 69, 70, 71], [72, 73, 74, 75, 76, 77, 78, 79, 80]]
    Partitioning a list of 82 elements into sublists of 9 elements
    10 sublists created
    [[0, 1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, 32, 33, 34, 35], [36, 37, 38, 39, 40, 41, 42, 43, 44], [45, 46, 47, 48, 49, 50, 51, 52, 53], [54, 55, 56, 57, 58, 59, 60, 61, 62], [63, 64, 65, 66, 67, 68, 69, 70, 71], [72, 73, 74, 75, 76, 77, 78, 79, 80], [81]]
    

    【讨论】:

      【解决方案4】:

      最简单的方法如下;

          List<Integer> initialList = List.of(0, 0, 1, 0, 1, 2, 0,
                      2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 7, 0,
                      7, 8, 0, 8, 9, 1, 0, 2, 1, 1, 3, 1, 2, 4, 1,
                      3, 5, 1, 4, 6, 1, 5, 7, 1, 6, 8, 1, 7, 9, 1,
                      8, 1, 2, 0, 3, 2, 1, 4, 2, 2, 5, 2, 3, 6, 2,
                      4, 7, 2, 5, 8, 2, 6, 9, 2, 7, 1, 2, 8, 2, 3,
                      0, 4, 3, 1, 5, 3, 2, 6, 3, 3, 7, 3, 4, 8, 3,
                      5, 9, 3, 6, 1, 3, 7, 2, 3, 8, 3, 4, 0, 5, 4,
                      1, 6, 4, 2, 7, 4, 3, 8, 4, 4, 9, 4, 5, 1, 4,
                      6, 2, 4, 7, 3, 4, 8, 4, 5, 0, 6, 5, 1, 7, 5,
                      2, 8, 5, 3, 9, 5, 4, 1, 5, 5, 2, 5, 6, 3, 5,
                      7, 4, 5, 8, 5, 6, 0, 7, 6, 1, 8, 6, 2, 9, 6,
                      3, 1, 6, 4, 2, 6, 5, 3, 6, 6, 4, 6, 7, 5, 6,
                      8, 6, 7, 0, 8, 7, 1, 9, 7, 2, 1, 7, 3, 2, 7,
                      4, 3, 7, 5, 4, 7, 6, 5, 7, 7, 6, 7, 8, 7, 8,
                      0, 9, 8, 1, 1, 8, 2, 2, 8, 3, 3, 8, 4, 4, 8,
                      5, 5, 8, 6, 6, 8, 7, 7, 8, 8, 8);
      
             List<List<Integer>> lists=   IntStream
                      .iterate(0, a -> a < initialList.size(),
                              a -> a + 9)
                      .mapToObj(a->new ArrayList<>(initialList.subList(a, a + 9)))
                      .collect(Collectors.toList());
      

      【讨论】:

        猜你喜欢
        • 2021-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-15
        • 1970-01-01
        • 1970-01-01
        • 2020-08-10
        • 2023-02-20
        相关资源
        最近更新 更多