Java Array Split splitArray(T[] elementArray, int maxSubArraySize)

Here you can find the source of splitArray(T[] elementArray, int maxSubArraySize)

Description

Splits an array in a list of subarrays of size maxSubArraySize.

License

Apache License

Parameter

Parameter Description
elementArray The array to be split.
maxSubArraySize The final size of the subarrays.

Return

The list containing the subarrays.

Declaration

public static <T> List<T[]> splitArray(T[] elementArray, int maxSubArraySize) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    /**/*from ww w  .j a va  2  s  .  c o  m*/
     * Splits an array in a list of subarrays of size <i>maxSubArraySize</i>.
     * @param elementArray The array to be split.
     * @param maxSubArraySize The final size of the subarrays.
     * @return The list containing the subarrays.
     */
    public static <T> List<T[]> splitArray(T[] elementArray, int 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);
            T[] slice = Arrays.copyOfRange(elementArray, indexFrom, indexTo);
            result.add(slice);
            slicedItems += slice.length;
            indexFrom = indexTo;
        }
        return result;
    }
}

Related

  1. splitAndPad(byte[] byteArray, int blocksize)
  2. splitArray(byte[] src, int size)
  3. splitArray(int[] array, int limit)
  4. splitArray(T[] array, int capacity)
  5. splitArray(T[] array, int max)
  6. splitByByteSequenceSeparator(String theString, byte[] separatorByteSequence)
  7. splitByteArray(byte[] array, int each)
  8. splitByteArray(byte[] inByteArray, int length)
  9. splitData(String[] sData, int numMaps)