Java Array Rotate rotateArray(Object[] array, int amt)

Here you can find the source of rotateArray(Object[] array, int amt)

Description

rotate Array

License

Open Source License

Declaration

public static Object[] rotateArray(Object[] array, int amt) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static Object[] rotateArray(Object[] array, int amt) {
        Object[] arr = Arrays.copyOf(array, array.length);
        if (arr == null || amt < 0) {
            throw new IllegalArgumentException("Illegal argument!");
        }//from   w  w w.  j  a v a  2  s. c  o  m

        for (int i = 0; i < amt; i++) {
            for (int j = arr.length - 1; j > 0; j--) {
                Object temp = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = temp;
            }
        }
        return arr;
    }

    public static float[] rotateArray(float[] array, int amt) {
        float[] arr = Arrays.copyOf(array, array.length);
        if (arr == null || amt < 0) {
            throw new IllegalArgumentException("Illegal argument!");
        }

        for (int i = 0; i < amt; i++) {
            for (int j = arr.length - 1; j > 0; j--) {
                float temp = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = temp;
            }
        }
        return arr;
    }
}

Related

  1. findAllPossibleRightRotations(int[][] matrix)
  2. rotateArrayRange(int[] array, int from, int to, int n)