Here you can find the source of rotate(final List
Parameter | Description |
---|---|
list | The <code>List</code> to rotate. |
n | The number of places to rotate by (positive or negative). |
public static <T> void rotate(final List<T> list, int n)
//package com.java2s; /**//from w w w .java 2 s .co m * Copyright (C) 2002-2015 The FreeCol Team * * This file is part of FreeCol. * * FreeCol is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * FreeCol is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with FreeCol. If not, see <http://www.gnu.org/licenses/>. */ import java.util.List; public class Main { /** * Rotate a list by N places. * * @param list The <code>List</code> to rotate. * @param n The number of places to rotate by (positive or negative). */ public static <T> void rotate(final List<T> list, int n) { final int len = list.size(); if (len <= 0 || n == 0) return; n %= len; if (n > 0) { for (; n > 0; n--) { T t = list.remove(0); list.add(t); } } else { for (; n < 0; n++) { T t = list.remove(n - 1); list.add(0, t); } } } }