For example : A list
A B C D E
Given C , Switch to
C A B D E
Notice that the array size will change, some items may removed in run times
Another solution, just keep swaping from 0 to indexOf(itemToMove).
This is my Kotlin version:
val list = mutableListOf('A', 'B', 'C', 'D', 'E')
(0..list.indexOf('C')).forEach {
Collections.swap(list, 0, it)
}
Sorry I am unfamiliar with Java but learned a little Kotlin. But the algorithm is the same.