Java Examples - Rotate Elements in List

Java 实例Java Examples

The following example demonstrates how to use the rotate() method of the Collections class to rotate elements, with the second parameter of the method specifying the starting position for the rotation:

Main.java file

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); } }

The output of running the above code is:

List :[one, Two, three, Four, five, six]
rotate: [Four, five, six, one, Two, three]

Java 实例Java Examples

Other Extensions