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

前端 未结 12 1934
春和景丽
春和景丽 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:35
       for(int i=0;i<list.size();){
        ArrayList<Integer>row = new ArrayList<Integer>();
        int k=0;
        while(k < chunksize){
            chunk.add(list.get(i));
            i++;
            k++;
        }
        System.out.println(chunk);
        nestedlist.add(chunk);
    }   
    

    where list is a 1 dimension array and chunk is a nested array of size chunksize

    0 讨论(0)
  • 2020-12-05 05:37
    public static int[][] chunkArray(int[] array, int chunkSize) {
            // first we need to get number of chunks by dividing length by chunkSize.
            int numOfChunks = (int)Math.ceil((double)array.length / chunkSize);
    // we declare 2d array to save in the chunks
            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;
        }
    
    0 讨论(0)
  • 2020-12-05 05:39

    Easy way to do so,

        int loopcount = employeeList.size() / constCount;
        int leftcount = employeeList.size() % constCount;
        for (int i = 0; i < loopcount - 1; i++) {
            //query.setParameterList("values"+i, employeeList.subList(tempCount, tempCount + constCount));
    
            System.out.println(employeeList.subList(tempCount, tempCount + constCount));
            tempCount = tempCount + 1000;
        }
    
    0 讨论(0)
  • 2020-12-05 05:40

    Just stumbled upon this post after encountering the same question. Here is how I solved it (I used Arrays.copyOfRange():

    public static int[][] splitArray(int[] arrayToSplit, int chunkSize){
        if(chunkSize<=0){
            return null;  // just in case :)
        }
        // first we have to check if the array can be split in multiple 
        // arrays of equal 'chunk' size
        int rest = arrayToSplit.length % chunkSize;  // if rest>0 then our last array will have less elements than the others 
        // then we check in how many arrays we can split our input array
        int chunks = arrayToSplit.length / chunkSize + (rest > 0 ? 1 : 0); // we may have to add an additional array for the 'rest'
        // now we know how many arrays we need and create our result array
        int[][] arrays = new int[chunks][];
        // we create our resulting arrays by copying the corresponding 
        // part from the input array. If we have a rest (rest>0), then
        // the last array will have less elements than the others. This 
        // needs to be handled separately, so we iterate 1 times less.
        for(int i = 0; i < (rest > 0 ? chunks - 1 : chunks); i++){
            // this copies 'chunk' times 'chunkSize' elements into a new array
            arrays[i] = Arrays.copyOfRange(arrayToSplit, i * chunkSize, i * chunkSize + chunkSize);
        }
        if(rest > 0){ // only when we have a rest
            // we copy the remaining elements into the last chunk
            arrays[chunks - 1] = Arrays.copyOfRange(arrayToSplit, (chunks - 1) * chunkSize, (chunks - 1) * chunkSize + rest);
        }
        return arrays; // that's it
    }
    

    And the results:

    chunkSize = 1
    [1]
    [2]
    [3]
    [4]
    [5]
    
    chunkSize = 2
    [1, 2]
    [3, 4]
    [5]
    
    chunkSize = 3
    [1, 2, 3]
    [4, 5]
    
    chunkSize = 4
    [1, 2, 3, 4]
    [5]
    
    chunkSize = 5
    [1, 2, 3, 4, 5]
    
    chunkSize = 6
    [1, 2, 3, 4, 5]
    
    0 讨论(0)
  • 2020-12-05 05:42
    public class ArrayChunk {
        public static void main(String[] args) {
    
            String[][] chunked = chunkArray("1,2,3,4,5,6,7,8,9".split(","), 2);
            System.out.println("Array with chunk size 2");
            Arrays.stream(chunked).forEach(strings -> System.out.println(String.join(",", strings)));
    
            chunked = chunkArray("1,2,3,4,5,6,7,8,9".split(","), 3);
            System.out.println("Array with chunk size 3");
            Arrays.stream(chunked).forEach(strings -> System.out.println(String.join(",", strings)));
    
            chunked = chunkArray("1,2,3,4,5,6,7,8,9".split(","), 4);
            System.out.println("Array with chunk size 4");
            Arrays.stream(chunked).forEach(strings -> System.out.println(String.join(",", strings)));
    
            chunked = chunkArray("1,2,3,4,5,6,7,8,9".split(","), 5);
            System.out.println("Array with chunk size 5");
            Arrays.stream(chunked).forEach(strings -> System.out.println(String.join(",", strings)));
        }
    
        private static String[][] chunkArray(String[] array, int chunkSize) {
            int chunkedSize = (int) Math.ceil((double) array.length / chunkSize); // chunked array size
            String[][] chunked = new String[chunkedSize][chunkSize];
            for (int index = 0; index < chunkedSize; index++) {
                String[] chunk = new String[chunkSize]; // small array
                System.arraycopy(array, index * chunkSize, chunk, 0, Math.min(chunkSize, array.length - index * chunkSize));
                chunked[index] = chunk;
            }
            return chunked;
        }
    }
    

    Output

    Array with chunk size 2
    1,2
    3,4
    5,6
    7,8
    9,null
    
    Array with chunk size 3
    1,2,3
    4,5,6
    7,8,9
    
    Array with chunk size 4
    1,2,3,4
    5,6,7,8
    9,null,null,null
    
    Array with chunk size 5
    1,2,3,4,5
    6,7,8,9,null
    
    
    0 讨论(0)
  • 2020-12-05 05:43

    In general you could use Arrays.copyOfRange to copy

    0 讨论(0)
提交回复
热议问题