Program to Rotate List Elements in Java
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]