rotate Array - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

rotate Array

Demo Code


//package com.java2s;

public class Main {
    public static void rotateArray(Integer[] arr, int index, int rotateBy) {

        for (int x = 0; x < arr.length; x++) {
            int newLocation = ((x + rotateBy) - 1) % arr.length;
            swapValues(arr, x, newLocation);
        }/*from w ww.j  a va2 s  . c o  m*/
    }

    /**
     * Will swap the values between the given input index params
     * 
     * @param array
     * @param index
     * @param otherIndex
     * 
     */
    public static <T> void swapValues(T array[], int index, int otherIndex) {
        T tempVal = array[otherIndex];
        array[otherIndex] = array[index];
        array[index] = tempVal;
    }
}

Related Tutorials