Java Example – Circularly Shifting List Elements
The following example demonstrates how to use the rotate() method of the Collections class to circularly shift elements. The second parameter specifies the starting position of the shift:
Main.java File
import
java
.
util
.*;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
List
list
=
Arrays
.
asList
(
"
one Two three Four five six
"
.
split
(
"
"
)
)
;
System
.
out
.
println
(
"
List :
"
list
)
;
Collections
.
rotate
(
list
,
3
)
;
System
.
out
.
println
(
"
rotate:
"
list
)
;
}
}
The output of the above code is:
List :[one, Two, three, Four, five, six]
rotate: [Four, five, six, one, Two, three]
Java Examples