Java Utililty Methods Array Split

List of utility methods to do Array Split

Description

The list of methods to do Array Split are organized into topic(s).

Method

ListsplitArray(T[] array, int capacity)
Split array
List<T[]> result = new ArrayList<T[]>();
for (int i = 0; i < array.length / capacity; i++) {
    T[] piece = Arrays.copyOfRange(array, i * capacity,
            ((i + 1) * capacity < array.length) ? (i + 1) * capacity : array.length);
    result.add(piece);
return result;
ListsplitArray(T[] array, int max)
split Array
int x = array.length / max;
int lower = 0;
int upper = 0;
List<T[]> list = new ArrayList<>();
for (int i = 0; i <= x; i++) {
    upper += max;
    list.add(Arrays.copyOfRange(array, lower, upper));
    lower = upper;
...
ListsplitArray(T[] elementArray, int maxSubArraySize)
Splits an array in a list of subarrays of size maxSubArraySize.
List<T[]> result = new ArrayList<T[]>();
if (elementArray == null || elementArray.length == 0)
    return result;
int indexFrom = 0;
int indexTo = 0;
int slicedItems = 0;
while (slicedItems < elementArray.length) {
    indexTo = indexFrom + Math.min(maxSubArraySize, elementArray.length - indexTo);
...
ListsplitByByteSequenceSeparator(String theString, byte[] separatorByteSequence)
This method tokenizes a given string using a given byte sequence as separator and returns a List containing found tokens.
List<String> tokenList = new ArrayList<String>();
if ((theString == null) || theString.equals("")) {
    return tokenList;
byte[] input = theString.getBytes();
if (Arrays.equals(input, separatorByteSequence)) {
    return tokenList;
if ((separatorByteSequence == null) || (separatorByteSequence.length == 0)) {
    tokenList.add(theString);
    return tokenList;
if (findByteSequenceWithinByteArray(input, separatorByteSequence) == -1) {
    tokenList.add(theString);
    return tokenList;
int startNextToken = 0;
int endNextToken = 0;
int maxPosition = input.length;
while (startNextToken < maxPosition) {
    endNextToken = findByteSequenceWithinByteArray(input, separatorByteSequence, startNextToken);
    if (endNextToken != -1) {
        if (endNextToken > startNextToken) {
            byte[] buffer = new byte[endNextToken - startNextToken];
            System.arraycopy(input, startNextToken, buffer, 0, buffer.length);
            String currToken = new String(buffer);
            tokenList.add(currToken);
        startNextToken = endNextToken + separatorByteSequence.length;
    } else {
        if (startNextToken != (maxPosition - 1)) {
            byte[] buffer = new byte[maxPosition - startNextToken];
            System.arraycopy(input, startNextToken, buffer, 0, buffer.length);
            String currToken = new String(buffer);
            tokenList.add(currToken);
            startNextToken = maxPosition;
return tokenList;
byte[][]splitByteArray(byte[] array, int each)
Split the byte array into pieces of the given size, the last piece may not have the same size of the others.
int slice = (int) Math.floor(array.length / each);
byte[][] slices = new byte[slice + 1][];
int read = 0;
for (int j1 = 0; j1 < slices.length; j1++) {
    int to = read + each;
    if (to > array.length) {
        to = array.length;
    slices[j1] = Arrays.copyOfRange(array, read, to);
    read += each;
return slices;
ListsplitByteArray(byte[] inByteArray, int length)
split Byte Array
if (inByteArray == null) {
    return null;
List<byte[]> rtnList = new ArrayList<byte[]>();
if (length >= inByteArray.length) {
    rtnList.add(inByteArray);
    rtnList.add(null);
    return rtnList;
...
String[][]splitData(String[] sData, int numMaps)
Split the data into numMaps splits
int nbInstances = sData.length;
int partitionSize = nbInstances / numMaps;
String[][] splits = new String[numMaps][];
for (int partition = 0; partition < numMaps; partition++) {
    int from = partition * partitionSize;
    int to = partition == (numMaps - 1) ? nbInstances : (partition + 1) * partitionSize;
    splits[partition] = Arrays.copyOfRange(sData, from, to);
return splits;
byte[]splitOrderByFirst(byte[] pattern, byte[] src, int offset)
split Order By First
byte[] result;
int match = firstMatch(pattern, src, offset);
if (match < 0) {
    result = null;
} else {
    if (match > 0) {
        result = Arrays.copyOfRange(src, offset, match);
    } else {
...
ListsplitSentences(String[] sentence, int limitLength, String reg)
split Sentences
List<String> listNewSentence = new ArrayList<String>();
for (int i = 0; i < sentence.length; i++) {
    String[] word = sentence[i].split(reg);
    if (word.length <= limitLength) {
        listNewSentence.add(sentence[i]);
    else {
        int resto = word.length % limitLength;
...
String[]splitStringOnFields(String in, int[] cuts)
split String On Fields
char[] data = in.toCharArray();
List holder = new ArrayList();
for (int i = 0; i < cuts.length; i++) {
    int start = cuts[i];
    int count = data.length - start;
    if (i < cuts.length - 1) {
        count = cuts[i + 1] - start;
    String item = new String(data, start, count);
    holder.add(item);
String[] ret = new String[holder.size()];
holder.toArray(ret);
return (ret);