Java Array Reverse reverseRange(final T[] arr, final int from, final int to)

Here you can find the source of reverseRange(final T[] arr, final int from, final int to)

Description

Reverses the order of a range of the array in place.

License

Open Source License

Parameter

Parameter Description
T The array type.
arr The array.
from The lowest included index.
to The highest excluded index.

Declaration

public static <T> void reverseRange(final T[] arr, final int from, final int to) 

Method Source Code

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

public class Main {
    /**//from   w w  w .  jav  a2  s  .  c om
     * Reverses the order of a range of the array in place.
     *
     * @param <T> The array type.
     * @param arr The array.
     * @param from The lowest included index.
     * @param to The highest excluded index.
     */
    public static <T> void reverseRange(final T[] arr, final int from, final int to) {
        final int mid = (to - from) / 2 + from;
        for (int i = from; i < mid; ++i) {
            swap(arr, i, to - i - 1);
        }
    }

    /**
     * Swaps two entries in a <code>T</code> array. For lists use
     * {@link java.util.Collections#swap(List, int, int)}.
     *
     * @param <T> The type of the array.
     * @param arr array
     * @param i position of first element
     * @param j position of second element
     */
    public static <T> void swap(final T[] arr, final int i, final int j) {
        final T temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    /**
     * Swaps two entries in a <code>double</code> array.
     *
     * @param arr array
     * @param i position of first element
     * @param j position of second element
     * @see #swap(Object[], int, int)
     */
    public static void swap(final double[] arr, final int i, final int j) {
        final double temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    /**
     * Swaps two entries in an <code>int</code> array.
     *
     * @param arr array
     * @param i position of first element
     * @param j position of second element
     * @see #swap(Object[], int, int)
     */
    public static void swap(final int[] arr, final int i, final int j) {
        final int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

Related

  1. reverseArray(final T[] arr)
  2. reverseArray(Object[] arr)
  3. reverseArray(T[] arr)
  4. reverseArrayList(ArrayList listIn)
  5. reverseList (ArrayList l)
  6. reverseSortedSet(int[] ints)
  7. sortReversed(final int[] a)

  8. HOME | Copyright © www.java2s.com 2016