Java Array Split splitArray(int[] array, int limit)

Here you can find the source of splitArray(int[] array, int limit)

Description

split Array

License

Open Source License

Declaration

public static List<int[]> splitArray(int[] array, int limit) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static List<int[]> splitArray(int[] array, int limit) {
        List<int[]> list = new ArrayList<>();
        if (array.length <= limit) {
            list.add(array);/*from  w  ww . j av  a  2s. c om*/
            return list;
        }
        int count = getSplitCount(array.length, limit);
        int copied = 0;
        for (int i = 0; i < count; i++) {
            list.add(Arrays.copyOfRange(array, copied, Math.min(array.length, copied + limit)));
            copied += limit;
        }
        return list;
    }

    public static int getSplitCount(int length, int maxlength) {
        int count = length / maxlength;
        if ((length % maxlength) != 0) {
            count++;
        }
        return count;
    }
}

Related

  1. split(final int[] array)
  2. split(int splitBefore, byte[] source)
  3. split(String string, String delim, String[] a)
  4. splitAndPad(byte[] byteArray, int blocksize)
  5. splitArray(byte[] src, int size)
  6. splitArray(T[] array, int capacity)
  7. splitArray(T[] array, int max)
  8. splitArray(T[] elementArray, int maxSubArraySize)
  9. splitByByteSequenceSeparator(String theString, byte[] separatorByteSequence)