集册 Java实例教程 旋转数组

旋转数组

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

451
旋转数组
//时   代    Java - nowjava.com 提 供

//package com.nowjava;


public class Main {

    /** Rotate an array, see std::rotate */

    public static <T> void rotate(T[] ray, int first, int new_first,

            int last) {

        int next = new_first;

        while (next != first) {

            T temp = ray[first];

            ray[first] = ray[next];

            ray[next] = temp;

            first++;

            next++;
            /**
             from
            * 时 代 J a v a 公 众 号 
            **/

            if (next == last) {

                next = new_first;

            } else if (first == new_first) {

                new_first = next;

            }

        }

    }


    /** Rotate an array, see std::rotate */

    public static void rotate(int[] ray, int first, int new_first, int last) {

        int next = new_first;

        while (next != first) {

            int temp = ray[first];

            ray[first] = ray[next];

            ray[next] = temp;

            first++;

            next++;

            if (next == last) {

                next = new_first;

            } else if (first == new_first) {

                new_first = next;

            }

        }

    }


    /** Rotate an array, see std::rotate */

    public static void rotate(float[] ray, int first, int new_first,

            int last) {

        int next = new_first;

        while (next != first) {

            
展开阅读全文