Java Array Split split(final int[] array)

Here you can find the source of split(final int[] array)

Description

Splits array into two smaller arrays.

License

Open Source License

Parameter

Parameter Description
array array to split

Return

two arrays as a split result, 0 index - left part (equal by length or bigger then right part), 1 index - right part.

Declaration

static int[][] split(final int[] array) 

Method Source Code


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

import java.util.Arrays;

public class Main {
    /**/*from  ww w  .  java 2  s  . c o  m*/
     * Splits array into two smaller arrays. If the array length is odd then the right part (0 index) will always be bigger.
     * Example: array 2, 1, 3, 5, 4 will be splitted into 2, 1 and 3, 5, 4,
     *
     * @param array array to split
     * @return two arrays as a split result, 0 index - left part (equal by length or bigger then right part), 1 index - right part.
     */
    static int[][] split(final int[] array) {
        final int[] part1 = Arrays.copyOfRange(array, 0, array.length / 2);
        final int[] part2 = Arrays.copyOfRange(array, array.length / 2, array.length);

        return twoDimensionalArray(part1, part2);
    }

    /**
     * Creates one two-dimensional array from two one-dimensional arrays.
     *
     * @param array1 first part of two dimensional array (at index 0)
     * @param array2 second part of two dimensional array (at index 1)
     * @return two dimensional array from array1 and array2
     */
    static int[][] twoDimensionalArray(final int[] array1, final int[] array2) {
        final int[][] result = new int[2][];
        result[0] = array1;
        result[1] = array2;

        return result;
    }
}

Related

  1. split(byte[] array, int index, int len, byte value)
  2. split(byte[] pattern, byte[] src)
  3. split(byte[] source, int c)
  4. split(Collection collection, C[] array, int pageSize)
  5. split(int splitBefore, byte[] source)
  6. split(String string, String delim, String[] a)
  7. splitAndPad(byte[] byteArray, int blocksize)
  8. splitArray(byte[] src, int size)