Java List Rotate rotate(List data, int n)

Here you can find the source of rotate(List data, int n)

Description

rotate

License

Open Source License

Declaration

public static <T> List<T> rotate(List<T> data, int n) 

Method Source Code


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

import java.util.*;

public class Main {
    public static <T> List<T> rotate(List<T> data, int n) {
        List<T> result = new ArrayList<>();
        int len = n % data.size() + ((n < 0) ? data.size() : 0);

        for (int i = 0; i < len; i++)
            result.add(data.get(data.size() + i - len));

        for (int i = len; i < data.size(); i++)
            result.add(data.get(i - len));

        return result;
    }/* ww  w . j av  a  2  s .co m*/
}

Related

  1. rotate(final List list, int n)
  2. rotate(List a)
  3. rotateRowsOfMatrix(List> matrix)

  4. HOME | Copyright © www.java2s.com 2016