Here you can find the source of reverse(final int[] array, int startIndexInclusive, int endIndexExclusive)
Reverses the order of the given array in the given range.
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. |
public static void reverse(final int[] array, int startIndexInclusive, int endIndexExclusive)
//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 (<0) is * promoted to 0, overvalue (>array.length) results in no * change. * @param 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. * @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++; } } }