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

前端 未结 12 1933
春和景丽
春和景丽 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:26

    Using pure Java 8:

    public class Chunk {
        public static void main(String[] args) {
            int[] input = {1,2,3,4,78,999,-1,456};
            int chunkSize = 3;
    
            int[][] chunked = chunk(input, chunkSize);
    
            Arrays.stream(chunked)
                    .map(Arrays::toString)
                        .forEach(System.out::println);
        }
    
        public static int[][] chunk(int[] input, int chunkSize) {
            return IntStream.iterate(0, i -> i + chunkSize)
                    .limit((long) Math.ceil((double) input.length / chunkSize))
                    .mapToObj(j -> Arrays.copyOfRange(input, j, j + chunkSize > input.length ? input.length : j + chunkSize))
                    .toArray(int[][]::new);
        }
    }
    
    [1, 2, 3]
    [4, 78, 999]
    [-1, 456]
    
    0 讨论(0)
  • 2020-12-05 05:30

    You can use Arrays.copyOfRange(int[] original, int from, int to) The code could be something like this:

    int chunk = 2; // chunk size to divide
    for(int i=0;i<original.length;i+=chunk){
        System.out.println(Arrays.toString(Arrays.copyOfRange(original, i, Math.min(original.length,i+chunk))));
    }          
    
    0 讨论(0)
  • 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<Integer> listToBeSplit = Arrays.asList(sourceArray);
    int chunkSize = 3;
    Lists.partition(listToBeSplit, chunkSize);
    
    0 讨论(0)
  • 2020-12-05 05:31
            import java.util.Arrays;
    
            public class ArrayChunk {
    
                public static void main(String[] args) {
    
                    int[] array = {1,2,3,4,5};
    
                    
                     int[][] chunks1 = ArrayChunk(array, 1);
                     print(chunks1);
                     int[][] chunks2 = ArrayChunk(array, 2);
                     print(chunks2);
                     int[][] chunks3 = ArrayChunk(array, 3);
                     print(chunks3);
    
                     
                     
                }
    
                public static int[][] ArrayChunk(int[] array, int chunkSize) {
                    int numOfChunks = (int) Math.ceil((double) array.length / chunkSize);
                    int[][] output = new int[numOfChunks][];
    
                    for (int i = 0; i < numOfChunks; i++) {
                        int start = i * chunkSize;
                        int length = Math.min(array.length - start, chunkSize);
    
                        int[] temp = new int[length];
                        System.arraycopy(array, start, temp, 0, length);
                        output[i] = temp;
                    }
    
                    //
                    return output;
                }
    
                private static void print(int[][] output) {
                    //
                    System.out.println("======================");
                    for (int[] x : output)
                        System.out.println(Arrays.toString(x));
                }
                
    
            }
    
    
    
            ======================
            [1]
            [2]
            [3]
            [4]
            [5]
            ======================
            [1, 2]
            [3, 4]
            [5]
            ======================
            [1, 2, 3]
            [4, 5]
    
    0 讨论(0)
  • 2020-12-05 05:34

    This should do the trick

    public static List<String> split(String string, int chunk) {
        Pattern pattern = Pattern.compile("(([0-9]+,){" + (chunk - 1)
                + "}[0-9]+)|[0-9]+");
        Matcher matcher = pattern.matcher(string);
    
        List<String> result = new ArrayList<String>();
        while (matcher.find())
            result.add("[" + matcher.group() + "]");
    
        return result;
    }
    

    Test code:

    for (int chunkSize = 1; chunkSize < 6; ++chunkSize) {
        System.out.println("test for chunk size: " + chunkSize);
        for (String string : split("[1,2,3,4,5]", chunkSize))
            System.out.format("\t%s\n", string);
    }
    

    Output:

    test for chunk size: 1
        [1]
        [2]
        [3]
        [4]
        [5]
    test for chunk size: 2
        [1,2]
        [3,4]
        [5]
    test for chunk size: 3
        [1,2,3]
        [4]
        [5]
    test for chunk size: 4
        [1,2,3,4]
        [5]
    test for chunk size: 5
        [1,2,3,4,5]
    
    0 讨论(0)
  • 2020-12-05 05:35

    Try this,

    Iterate it and check to give the chunk size.

    int chunkSize = userInput;
    

    // iterate and check the condition

    char[] resultArray = Arrays.copyOfRange(inputArray, start, end);
    start = start + end;  // check whether the start will exceeds the length of the array
    
    0 讨论(0)
提交回复
热议问题