Java Array Rotate rotateArrayRange(int[] array, int from, int to, int n)

Here you can find the source of rotateArrayRange(int[] array, int from, int to, int n)

Description

rotate Array Range

License

Open Source License

Declaration

public static void rotateArrayRange(int[] array, int from, int to, int n) 

Method Source Code

//package com.java2s;

import java.util.Arrays;

public class Main {
    public static void rotateArrayRange(int[] array, int from, int to, int n) {
        rotateArrayRange(array, from, to, n, Arrays.copyOfRange(array, from, to));
    }//from w  ww.ja  va  2  s  .c o  m

    public static void rotateArrayRange(int[] array, int from, int to, int n, int[] copyOfRange) {
        final int d = to - from;
        if (n < 0)
            n = to - from + n;
        for (int i = 0; i < d; ++i)
            array[from + i] = copyOfRange[(i + n) % d];
    }

    public static <T> void rotateArrayRange(T[] array, int from, int to, int n) {
        rotateArrayRange(array, from, to, n, Arrays.copyOfRange(array, from, to));
    }

    public static <T> void rotateArrayRange(T[] array, int from, int to, int n, T[] copyOfRange) {
        final int d = to - from;
        if (n < 0)
            n = to - from + n;
        for (int i = 0; i < d; ++i)
            array[from + i] = copyOfRange[(i + n) % d];
    }
}

Related

  1. findAllPossibleRightRotations(int[][] matrix)
  2. rotateArray(Object[] array, int amt)