Java examples for Collection Framework:Array Reverse
Reverses the contents of the given array.
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { int[] arr = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; reverse(arr);/*from w w w .j a v a2 s . c o m*/ } /** * Reverses the contents of the given array. * @param arr */ public static void reverse(int[] arr) { // exchange arr[0] with arr[length - 1], arr[1] // with arr[length - 2], and so on, until we get // to the middle of the array int front = 0; int rear = arr.length - 1; while (front < rear) { // exchange arr[front] with arr[rear] using a temporary variable int temp = arr[front]; arr[front] = arr[rear]; arr[rear] = temp; // move indices towards the center front += 1; rear -= 1; } } }