Reverses the contents of the array. : Array Compare « Collections Data Structure « Java






Reverses the contents of the array.

      

/*
 * Copyright WizTools.org
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 */
//package org.wiztools.commons;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author subwiz
 */
public final class ArrayUtil {

    /**
     * Reverses the contents of the array.
     * @param <T>
     * @param arr The input array of objects.
     */
    public static <T> void reverse(T[] arr) {
        for(int left=0, right=arr.length-1; left < right; left++, right--) {
            final T tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }

    public static void reverse(boolean[] arr) {
        for(int left=0, right=arr.length-1; left < right; left++, right--) {
            final boolean tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }
    
    public static void reverse(byte[] arr) {
        for(int left=0, right=arr.length-1; left < right; left++, right--) {
            final byte tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }

    public static void reverse(char[] arr) {
        for(int left=0, right=arr.length-1; left < right; left++, right--) {
            final char tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }

    public static void reverse(short[] arr) {
        for(int left=0, right=arr.length-1; left < right; left++, right--) {
            final short tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }

    public static void reverse(int[] arr) {
        for(int left=0, right=arr.length-1; left < right; left++, right--) {
            final int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }

    public static void reverse(long[] arr) {
        for(int left=0, right=arr.length-1; left < right; left++, right--) {
            final long tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }

    public static void reverse(float[] arr) {
        for(int left=0, right=arr.length-1; left < right; left++, right--) {
            final float tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }

    public static void reverse(double[] arr) {
        for(int left=0, right=arr.length-1; left < right; left++, right--) {
            final double tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Check if a text is present at the current position in a buffer for byte array
2.Check if a text is present at the current position in a buffer(char array and char array)
3.Check if a text is present at the current position in a buffer(char array and string)
4.Returns true if all the references in array1 are equal to all the references in array2 (two null references are considered equal for this test).
5.Returns true if any two items in the array are equal to one another. Any null values in the array are ignored.
6.Tests two float arrays for equality.
7.Does the source array equal the match array
8.Does this byte array begin with match array content?
9.Compares the initial elements of two arrays.
10.String search and reflection helper methodsString search and reflection helper methods
11.Checks whether two arrays are the same length, treating null arrays as length 0.
12.Checks whether two arrays are the same type taking into account multi-dimensional arrays.
13.Get the maximum value in a double array
14.Compare equality of two two-dimensional boolean array
15.Two double value arrays are almost equal
16.Find the index of the array nearest to the value.
17.Retrive the quartile value from an array