A straightforward solution
If you only need to 1-rotate elements at 4 indices of a List
, you can just write a straightforward generic method like this:
static <T> void rotate4(List<T> list, int i0, int i1, int i2, int i3) {
T item = list.get(i3);
item = list.set(i0, item);
item = list.set(i1, item);
item = list.set(i2, item);
item = list.set(i3, item);
}
This will cyclically rotate 4 elements of any List<T>
. Remember that List.set
returns the element that previously was at that index, so you could write the entire method in one-line if you want:
// one-liner version
list.set(i3, list.set(i2, list.set(i1, list.set(i0, list.get(i3)))));
With this helper method, you'll have:
List<Character> list = Arrays.asList(
'a','b','c','d','e','f','g','h','i','j','k','l'
);
System.out.println(list);
// [a, b, c, d, e, f, g, h, i, j, k, l]
// * * * *
rotate4(list, 2, 5, 8, 11);
System.out.println(list);
// [a, b, l, d, e, c, g, h, f, j, k, i]
// * * * *
A more general solution
IF you need a way to rotate an arbitrary number of elements for an arbitrary distance, then you can create a live view of another List
, and then you can Collections.rotate
that view.
IF the elements are consecutive, for example, you'd just use subList
:
List<Character> list = Arrays.asList(
'a','b','c','d','e','f','g','h','i','j','k','l'
);
System.out.println(list);
// [a, b, c, d, e, f, g, h, i, j, k, l]
// * * * * *
System.out.println(list.subList(1, 6));
// [b, c, d, e, f]
Collections.rotate(list.subList(1, 6), -2);
System.out.println(list);
// [a, d, e, f, b, c, g, h, i, j, k, l]
// * * * * *
Since the elements aren't consecutive, you can't use subList
, but you can write e.g. PeriodicalLiveViewList
class. You want to be able to write something like this:
System.out.println(PeriodicalLiveViewList.of(list, 3, 2));
// [c, f, i, l]
Collections.rotate(PeriodicalLiveViewList.of(list, 3, 2), 1);
Basically you create another List
whose elements are every 3rd element of another List
, starting at index 2, as a live view.
If you are using Guava, there is ForwardingList
that you can built on. You can implement the decorator pattern for this from scratch too if necessary.
Related questions