Hey guys! I'm still a bit new to Java and I'm working on a problem where I'm supposed to rotate every n elements in an ArrayList.
The method takes in as parameters: List<Integers> list and an integer n.
if n = 3;
If the List contains -> [0,1,2,3,4,5,6,7,8,9,10]
After the call on rotateEveryN the list should be -> [2,0,1,5,3,4,8,6,7,9,10]
I know how to go about storing the nth element in a temp variable. And how to move elements
around. But I seem to be getting confused on how to properly rotate this subset of elements within the list.
public static void rotateEveryN(List<Integer> list, int n) { for (int i = 0; i < list.size(); i++){ for (int j = i; j <= n; j++){ // <- I know this is where my confusion is starting // How can I only work on three elements at a time from within the list? // Should the inner loop be starting back at 0 every n element? } } }