簡體   English   中英

如何在 Java 中將一個數組列表拆分為多個長度為 k 的數組列表? 我不想查看新列表,但我想創建它們

[英]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

所以,我已經閱讀了很多關於如何從多個數組列表創建一個 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);

但由於某種原因它不起作用,因為我無法在番石榴庫中導入 lists.partition() 方法

你可以這樣做:

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]

您可以手動遍歷每個元素,如果您的子列表有 9 個元素,則創建一個新元素(您可以用您喜歡的任何數字替換 9)。 並不是說如果您有許多不能被 9 整除的元素,則 listOfLists 中最后一個子列表的長度將 < 9。

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);
    }  
}

我會考慮使用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]]

最簡單的方法如下;

    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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM