How to rotate elements of the List?
SolutionFollowing example uses rotate() method to rotate elements of the list depending on the 2nd argument of the method.
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); } }Result
The above code sample will produce the following result.
List :[one, Two, three, Four, five, six] rotate: [Four, five, six, one, Two, three]
java_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4