Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java LinkedList method to use as a ring list?

If I have a LinkedList:

Value A B C D E F
Index 0 1 2 3 4 5

I want to reorder the list or an Iterator for example at D(index 3) to be the new head but keep the order like:

Value D E F A B C
Index 0 1 2 3 4 5

Is this a functionality that can be achieved with LinkedList out of the box, basically realizing a ring? Is there another class that offers this already implemented?

like image 377
Ben Avatar asked Apr 12 '26 23:04

Ben


1 Answers

You can use Collections.rotate for classes that implement the List interface:

List<String> list = Arrays.asList("A", "B", "C", "D", "E", "F");
Collections.rotate(list, 3);
System.out.println(list);

Output:

[D, E, F, A, B, C]

like image 160
Alex R Avatar answered Apr 15 '26 12:04

Alex R