Java Array Sub Array subarray(boolean[] array, int fromIndex, int length)

Here you can find the source of subarray(boolean[] array, int fromIndex, int length)

Description

Building on the arraycopy function in System, this function returns a "subarray" of a larger array.

License

Creative Commons License

Parameter

Parameter Description
array a parameter
fromIndex first element in returned array
length amount to read from array

Return

boolean array consisting of elements in range [fromIndex,fromIndex+length]

Declaration

public static boolean[] subarray(boolean[] array, int fromIndex, int length) 

Method Source Code

//package com.java2s;
/**/*  ww w.  j  av a 2 s.  co m*/
 * A simple class comprised of various tools used in handling of boolean arrays and values.
 * 
 * @author Raymond Berg www.rwberg.org
 * 
 * Use is made available under the Creative Commons License. 
 * Feel free to use but leave attribution to original author throughout use.
 */

public class Main {
    /**
     * Building on the arraycopy function in System, this function returns a
     * "subarray" of a larger array. The length parameter determines the 
     * length of the resulting array. Standard out of bounds exceptions on length 
     * can be expected.
     * 
     * @see {@link booleanUtil#subarray(boolean[], int)}
     * @param array
     * @param fromIndex
     *            first element in returned array
     * @param length
     *            amount to read from array
     * @return boolean array consisting of elements in range
     *         [fromIndex,fromIndex+length]
     */
    public static boolean[] subarray(boolean[] array, int fromIndex, int length) {
        boolean[] tempArray = new boolean[length];
        try {
            System.arraycopy(array, fromIndex, tempArray, 0, tempArray.length);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
            System.out.println("Array of length:" + array.length);
            System.out.println("From index with length:" + fromIndex + "," + length);
            System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        }
        return tempArray;
    }

    /**
     * Building on the arraycopy function in System, this function returns a
     * "subarray" of a larger array. Function returns subarray starting at 
     * fromIndex and ending with the final element of the array.
     *     * @see booleanUtil#subarray(boolean[], int, int)
     * @param array
     * @param fromIndex
     * @return
     */
    public static boolean[] subarray(boolean[] array, int fromIndex) {
        boolean[] tempArray = new boolean[array.length - fromIndex];
        System.arraycopy(array, fromIndex, tempArray, 0, tempArray.length);
        return tempArray;
    }
}

Related

  1. sub(String[] a, String[] b)
  2. sub(T[] source, int first, int last)
  3. subArray(byte[] a, int beginIndex, int endIndex)
  4. subArray(byte[] array, int beginIndex, int endIndex)
  5. subarray(byte[] array, int offset, int length)
  6. subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)