Program to Rotate List Elements in Java
This example shows how to rotate list elements in Java. The rotate method of the Collections class is used to rotate the elements of the list.
Following example uses rotate() method to rotate elements of the list depending on the 2nd argument of the method.
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.*; public class Main { public static void main(String[] args) { List list = Arrays.asList("one Two three Four five six".split(" ")); System.out.println("List :" + list); Collections.rotate(list, 3); System.out.println("rotate: " + list); } } |
Output:
List :[one, Two, three, Four, five, six]
rotate: [Four, five, six, one, Two, three]
rotate: [Four, five, six, one, Two, three]