How to split a string array into small chunk arrays in java?
47,446
Solution 1
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))));
}
Solution 2
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]
Solution 3
If you don't mind importing Google Guava and converting to a List, there is a method for partitioning Lists:
The following may achieve the desired result:
List<Integer> listToBeSplit = Arrays.asList(sourceArray);
int chunkSize = 3;
Lists.partition(listToBeSplit, chunkSize);
Solution 4
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]
Solution 5
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]

Author by
user2323036
Updated on July 09, 2022Comments
-
user2323036 11 months
Below is the example of the code snippet which needs the help
Example:
[1,2,3,4,5]
- if the chunk size is
1
,[1,2,3,4,5]
- if the chunk size is
2
,[1,2]
and[3,4]
and[5]
- if the chunk size is
3
,[1,2,3]
and[4,5]
- if the chunk size is
4
,[1,2,3,4]
and[5]
Java (from comment):
int counter = 0; for (int i=0; i<array.length; i++) { if (count == chunksize) { //do something and initialize counter = 0; } counter++; }
- if the chunk size is