rotates the item in list - Java java.util

Java examples for java.util:List Operation

Description

rotates the item in list

Demo Code


//package com.java2s;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "java2s.com");
        int fromPos = 42;
        int toPos = 42;
        rotateInto(list, fromPos, toPos);
    }//from  ww  w  . j  a  v  a 2 s .c  o m
    public static void rotateInto(List<?> list, int fromPos, int toPos) {
        boolean moveRight = toPos > fromPos;

        int fromIndex = moveRight ? fromPos : toPos;
        int toIndex = (moveRight ? toPos : fromPos) + 1;
        int distance = moveRight ? -1 : 1; // negative == backwards

        Collections.rotate(list.subList(fromIndex, toIndex), distance);

    }
}

Related Tutorials