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
Let say you have an array:
String[] arrayOne = new String[]{"A","B","C","D","E"};
Now you want to place the C
at index 0
get the C
in another variable
String characterC = arrayOne[2];
Now run the loop like following:
for (int i = (2 - 1); i >= 0; i--) {
arrayOne[i+1] = arrayOne[i];
}
Above 2
is index of C
. Now insert C
at index for example on 0
arrayOne[0] = characterC;
Result of above loop will be like that:
arrayOne: {"C","A","B","D","E"}
The end, we achieve our goal.