集册 Java实例教程 反转给定数组的内容。

反转给定数组的内容。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

381
反转给定数组的内容。


//package com.nowjava;


public class Main {// 来自 nowjava.com - 时代Java

    public static void main(String[] argv) throws Exception {

        int[] arr = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };

        reverse(arr);

    }


    /**

     * 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) {//来自 时 代 J a v a - N o w J a v a . c o m

            // exchange arr[front] with arr[rear] using a temporary variable

            
展开阅读全文