Collections.rotate(List, int): This method is available in java.util.Collections class of Java.
Syntax:
void java.util.Collections.rotate(List<?> list, int distance)
This method takes two arguments. This method rotates the elements in the specified list by the specified distance.
Note: After calling this method, the element at index i will be the element previously at index (i - distance) mod list.size(), for all values of i between 0 and list.size()-1, inclusive.
Parameters: Two parameters are required for this method.
list: the list to be rotated.
distance: the distance to rotate the list. There are no constraints on this value; it may be zero, negative, or greater than list.size().
Throws:
UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.
Approach
Java
import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Collectionsrotate {public static void main(String[] args) {List<Integer> arrlist = new ArrayList<Integer>();arrlist.add(12);arrlist.add(56);arrlist.add(899);arrlist.add(65);arrlist.add(5);int distance = 2;Collections.rotate(arrlist, distance);System.out.println(arrlist);}}
Output:
[65, 5, 12, 56, 899]
No comments:
Post a Comment