-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate.java
More file actions
30 lines (23 loc) · 776 Bytes
/
rotate.java
File metadata and controls
30 lines (23 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* Write a function that rotates the elements of an ArrayList by a given number of positions. For example, if the ArrayList is rotated by 2 positions, the elements should shift right by 2 places.
Input:
ArrayList: [10, 20, 30, 40, 50]
Rotate by: 2
Output: [40, 50, 10, 20, 30]
*/
import java.util.ArrayList;
import java.util.Collections;
public class rotate {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
int rotateBy = 2;
System.out.println("Before rotation: " + list);
Collections.rotate(list, rotateBy);
System.out.println("After rotation: " + list);
}
}