Java Array Slice slice(T[] array, int start, int finish)

Here you can find the source of slice(T[] array, int start, int finish)

Description

Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.

License

Open Source License

Parameter

Parameter Description
T The array type
array The array to be sliced. Note that the original array remains unchanged.
start The starting node.
finish The ending node (inclusive).

Declaration

public static <T> T[] slice(T[] array, int start, int finish) 

Method Source Code

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

public class Main {
    /**//from w w w.j  ava  2 s  .c om
     * Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.
     * That is, start and finish are inclusive. Finish may be less than start, in which case the slice will also
     * be backwards, so slicing from 1 to 0 would return [1, 0]. If start and finish are equal, an array with one
     * result is returned.
     * @param <T> The array type
     * @param array The array to be sliced. Note that the original array remains unchanged.
     * @param start The starting node.
     * @param finish The ending node (inclusive).
     * @return 
     */
    public static <T> T[] slice(T[] array, int start, int finish) {
        int size = Math.abs(start - finish) + 1;
        Object[] newArray = new Object[size];
        if (start <= finish) {
            int counter = 0;
            for (int i = start; i <= finish; i++) {
                newArray[counter++] = array[i];
            }
        } else {
            int counter = 0;
            for (int i = start; i >= finish; i--) {
                newArray[counter++] = array[i];
            }
        }
        return (T[]) newArray;
    }

    /**
     * Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.
     * That is, start and finish are inclusive. Finish may be less than start, in which case the slice will also
     * be backwards, so slicing from 1 to 0 would return [1, 0]. If start and finish are equal, an array with one
     * result is returned.
     * @param array The array to be sliced. Note that the original array remains unchanged.
     * @param start The starting node.
     * @param finish The ending node (inclusive).
     * @return 
     */
    public static char[] slice(char[] array, int start, int finish) {
        int size = Math.abs(start - finish) + 1;
        char[] newArray = new char[size];
        if (start <= finish) {
            int counter = 0;
            for (int i = start; i <= finish; i++) {
                newArray[counter++] = array[i];
            }
        } else {
            int counter = 0;
            for (int i = start; i >= finish; i--) {
                newArray[counter++] = array[i];
            }
        }
        return newArray;
    }

    /**
     * Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.
     * That is, start and finish are inclusive. Finish may be less than start, in which case the slice will also
     * be backwards, so slicing from 1 to 0 would return [1, 0]. If start and finish are equal, an array with one
     * result is returned.
     * @param array The array to be sliced. Note that the original array remains unchanged.
     * @param start The starting node.
     * @param finish The ending node (inclusive).
     * @return 
     */
    public static byte[] slice(byte[] array, int start, int finish) {
        int size = Math.abs(start - finish) + 1;
        byte[] newArray = new byte[size];
        if (start <= finish) {
            int counter = 0;
            for (int i = start; i <= finish; i++) {
                newArray[counter++] = array[i];
            }
        } else {
            int counter = 0;
            for (int i = start; i >= finish; i--) {
                newArray[counter++] = array[i];
            }
        }
        return newArray;
    }

    /**
     * Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.
     * That is, start and finish are inclusive. Finish may be less than start, in which case the slice will also
     * be backwards, so slicing from 1 to 0 would return [1, 0]. If start and finish are equal, an array with one
     * result is returned.
     * @param array The array to be sliced. Note that the original array remains unchanged.
     * @param start The starting node.
     * @param finish The ending node (inclusive).
     * @return 
     */
    public static short[] slice(short[] array, int start, int finish) {
        int size = Math.abs(start - finish) + 1;
        short[] newArray = new short[size];
        if (start <= finish) {
            int counter = 0;
            for (int i = start; i <= finish; i++) {
                newArray[counter++] = array[i];
            }
        } else {
            int counter = 0;
            for (int i = start; i >= finish; i--) {
                newArray[counter++] = array[i];
            }
        }
        return newArray;
    }

    /**
     * Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.
     * That is, start and finish are inclusive. Finish may be less than start, in which case the slice will also
     * be backwards, so slicing from 1 to 0 would return [1, 0]. If start and finish are equal, an array with one
     * result is returned.
     * @param array The array to be sliced. Note that the original array remains unchanged.
     * @param start The starting node.
     * @param finish The ending node (inclusive).
     * @return 
     */
    public static int[] slice(int[] array, int start, int finish) {
        int size = Math.abs(start - finish) + 1;
        int[] newArray = new int[size];
        if (start <= finish) {
            int counter = 0;
            for (int i = start; i <= finish; i++) {
                newArray[counter++] = array[i];
            }
        } else {
            int counter = 0;
            for (int i = start; i >= finish; i--) {
                newArray[counter++] = array[i];
            }
        }
        return newArray;
    }

    /**
     * Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.
     * That is, start and finish are inclusive. Finish may be less than start, in which case the slice will also
     * be backwards, so slicing from 1 to 0 would return [1, 0]. If start and finish are equal, an array with one
     * result is returned.
     * @param array The array to be sliced. Note that the original array remains unchanged.
     * @param start The starting node.
     * @param finish The ending node (inclusive).
     * @return 
     */
    public static long[] slice(long[] array, int start, int finish) {
        int size = Math.abs(start - finish) + 1;
        long[] newArray = new long[size];
        if (start <= finish) {
            int counter = 0;
            for (int i = start; i <= finish; i++) {
                newArray[counter++] = array[i];
            }
        } else {
            int counter = 0;
            for (int i = start; i >= finish; i--) {
                newArray[counter++] = array[i];
            }
        }
        return newArray;
    }

    /**
     * Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.
     * That is, start and finish are inclusive. Finish may be less than start, in which case the slice will also
     * be backwards, so slicing from 1 to 0 would return [1, 0]. If start and finish are equal, an array with one
     * result is returned.
     * @param array The array to be sliced. Note that the original array remains unchanged.
     * @param start The starting node.
     * @param finish The ending node (inclusive).
     * @return 
     */
    public static float[] slice(float[] array, int start, int finish) {
        int size = Math.abs(start - finish) + 1;
        float[] newArray = new float[size];
        if (start <= finish) {
            int counter = 0;
            for (int i = start; i <= finish; i++) {
                newArray[counter++] = array[i];
            }
        } else {
            int counter = 0;
            for (int i = start; i >= finish; i--) {
                newArray[counter++] = array[i];
            }
        }
        return newArray;
    }

    /**
     * Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.
     * That is, start and finish are inclusive. Finish may be less than start, in which case the slice will also
     * be backwards, so slicing from 1 to 0 would return [1, 0]. If start and finish are equal, an array with one
     * result is returned.
     * @param array The array to be sliced. Note that the original array remains unchanged.
     * @param start The starting node.
     * @param finish The ending node (inclusive).
     * @return 
     */
    public static double[] slice(double[] array, int start, int finish) {
        int size = Math.abs(start - finish) + 1;
        double[] newArray = new double[size];
        if (start <= finish) {
            int counter = 0;
            for (int i = start; i <= finish; i++) {
                newArray[counter++] = array[i];
            }
        } else {
            int counter = 0;
            for (int i = start; i >= finish; i--) {
                newArray[counter++] = array[i];
            }
        }
        return newArray;
    }

    /**
     * Slices an array, where the array [0, 1, 2] sliced from 0 to 2 would return the whole array.
     * That is, start and finish are inclusive. Finish may be less than start, in which case the slice will also
     * be backwards, so slicing from 1 to 0 would return [1, 0]. If start and finish are equal, an array with one
     * result is returned.
     * @param array The array to be sliced. Note that the original array remains unchanged.
     * @param start The starting node.
     * @param finish The ending node (inclusive).
     * @return 
     */
    public static boolean[] slice(boolean[] array, int start, int finish) {
        int size = Math.abs(start - finish) + 1;
        boolean[] newArray = new boolean[size];
        if (start <= finish) {
            int counter = 0;
            for (int i = start; i <= finish; i++) {
                newArray[counter++] = array[i];
            }
        } else {
            int counter = 0;
            for (int i = start; i >= finish; i--) {
                newArray[counter++] = array[i];
            }
        }
        return newArray;
    }
}

Related

  1. slice(String[] o, int index)
  2. slice(String[] strings, int begin, int length)
  3. slice(String[] strings, int begin, int length)
  4. slice(T[] array, int index)
  5. slice(T[] array, int index, int length)
  6. slice(T[] items, int offset, int length)
  7. sliceArray(final String[] array, final int start)
  8. SliceByteArray(byte data[], int offset, int length)
  9. sliceBytes(byte[] bytes, int offset, int length)