Android Array Reverse reverse(final int[] array, int startIndexInclusive, int endIndexExclusive)

Here you can find the source of reverse(final int[] array, int startIndexInclusive, int endIndexExclusive)

Description

Reverses the order of the given array in the given range.

License

Open Source License

Parameter

Parameter Description
array the array to reverse, may be null
startIndexInclusive the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no change.
endIndexExclusive elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no change. Overvalue (>array.length) is demoted to array length.

Declaration

public static void reverse(final int[] array, int startIndexInclusive,
        int endIndexExclusive) 

Method Source Code

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

public class Main {
    /**/*from   ww  w  . j  a  va2  s  .co m*/
     * <p>
     * Reverses the order of the given array.
     * </p>
     * <p>
     * This method does nothing for a {@code null} input array.
     * </p>
     * 
     * @param array the array to reverse, may be {@code null}
     */
    public static void reverse(final int[] array) {
        if (array == null) {
            return;
        }
        reverse(array, 0, array.length);
    }

    /**
     * <p>
     * Reverses the order of the given array in the given range.
     * </p>
     * <p>
     * This method does nothing for a {@code null} input array.
     * </p>
     * 
     * @param array the array to reverse, may be {@code null}
     * @param startIndexInclusive the starting index. Undervalue (&lt;0) is
     *            promoted to 0, overvalue (&gt;array.length) results in no
     *            change.
     * @param endIndexExclusive elements up to endIndex-1 are reversed in the
     *            array. Undervalue (&lt; start index) results in no change.
     *            Overvalue (&gt;array.length) is demoted to array length.
     * @since 3.2
     */
    public static void reverse(final int[] array, int startIndexInclusive,
            int endIndexExclusive) {
        if (array == null) {
            return;
        }
        int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
        int j = Math.min(array.length, endIndexExclusive) - 1;
        int tmp;
        while (j > i) {
            tmp = array[j];
            array[j] = array[i];
            array[i] = tmp;
            j--;
            i++;
        }
    }
}

Related

  1. reverse(final double[] pArray)
  2. reverse(final double[] pArray)
  3. reverse(final float[] pArray)
  4. reverse(final float[] pArray)
  5. reverse(final int[] array)
  6. reverse(final int[] pArray)
  7. reverse(final int[] pArray)
  8. reverse(final long[] pArray)
  9. reverse(final long[] pArray)