Android Array Reverse reverse(final int[] array)

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

Description

Reverses the order of the given array.

License

Open Source License

Parameter

Parameter Description
array the array to reverse, may be null

Declaration

public static void reverse(final int[] array) 

Method Source Code

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

public class Main {
    /**//from   ww  w  .  j  a v a  2 s  .c om
     * <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 byte[] pArray)
  2. reverse(final double[] pArray)
  3. reverse(final double[] pArray)
  4. reverse(final float[] pArray)
  5. reverse(final float[] pArray)
  6. reverse(final int[] array, int startIndexInclusive, int endIndexExclusive)
  7. reverse(final int[] pArray)
  8. reverse(final int[] pArray)
  9. reverse(final long[] pArray)