如何在java中将字符串数组拆分为小块数组?

use*_*036 19 java

以下是需要帮助的代码段示例

例:

[1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
  • 如果块大小是1,[1,2,3,4,5]
  • 如果块大小是2,[1,2][3,4][5]
  • 如果块大小是3,[1,2,3][4,5]
  • 如果块大小是4,[1,2,3,4][5]

Java(来自评论):

int counter = 0;
for (int i=0; i<array.length; i++) {
  if (count == chunksize) {
    //do something and initialize
    counter = 0;
  }
  counter++; 
}
Run Code Online (Sandbox Code Playgroud)

Rez*_*eza 20

您可以使用Arrays.copyOfRange(int[] original, int from, int to) 代码可能是这样的:

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))));
}          
Run Code Online (Sandbox Code Playgroud)


Gam*_*ids 17

在遇到同样的问题后偶然发现了这篇文章.这是我解决它的方式(我用过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
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

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]
Run Code Online (Sandbox Code Playgroud)

  • 扎实落实。实际上对当前的问题给出了完整且正确的答案。 (4认同)

Tim*_*Tim 9

如果你不介意导入Google Guava并转换成List,这里有一个Lists分区的方法:

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

以下可能达到预期的结果:

List<Integer> listToBeSplit = Arrays.asList(sourceArray);
int chunkSize = 3;
Lists.partition(listToBeSplit, chunkSize);
Run Code Online (Sandbox Code Playgroud)

  • 还有一个 Apache Commons Collections ListUtils.partition() 方法。 (2认同)

rhe*_*ser 5

使用纯 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]
Run Code Online (Sandbox Code Playgroud)