How to split a string array into small chunk arrays in java?

前端 未结 12 1955
春和景丽
春和景丽 2020-12-05 05:15

Below is the example of the code snippet which needs the help

Example:

[1,2,3,4,5]
  • if the chunk size is 1,
12条回答
  •  长情又很酷
    2020-12-05 05:31

    If you don't mind importing Google Guava and converting to a List, there is a method for partitioning Lists:

    https://google.github.io/guava/releases/27.1-jre/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-

    The following may achieve the desired result:

    List listToBeSplit = Arrays.asList(sourceArray);
    int chunkSize = 3;
    Lists.partition(listToBeSplit, chunkSize);
    

提交回复
热议问题