Rearranging the Elements of an Array

A reversed copy
A bus route is stored as an array of stop names, in the order the bus reaches them. Each section below starts from the route as declared here.
String[] route = {"Depot", "Library", "Market", "Pier", "Stadium"};
The return trip visits the same stops in the opposite order, and a new array can hold it.
String[] back = new String[route.length];
int j = route.length - 1;
for (int i = 0; i < route.length; i++)
{
back[i] = route[j];
j--;
}
As i climbs from 0 to 4, j falls from 4 to 0, so back holds the stops from "Stadium" to "Depot". Nothing is assigned to an element of route, so the original order is still there.
Swapping two elements
Exchanging two elements takes a third variable. These statements swap the second and third stops.
String held = route[1];
route[1] = route[2];
route[2] = held;
After the second statement, both elements hold "Market", and "Library" survives only in held. The third statement puts it at index 2. Without held, the assignment to route[1] would destroy "Library" before anything could copy it.
Reversing in place
Reversing the array itself swaps the outermost pair of elements, then the pair just inside it, working toward the middle.
int last = route.length - 1;
for (int i = 0; i < route.length / 2; i++)
{
String held = route[i];
route[i] = route[last - i];
route[last - i] = held;
}
Each pass puts two elements in their final places. Five stops need two swaps, index 0 with 4 and index 1 with 3, and route.length / 2 is 2, so the loop makes exactly those passes. The middle stop, "Market", is already where a reversal puts it. A loop that ran all five passes would swap each pair a second time and leave the route as it began.
Rotating
On a loop route the bus can start anywhere. Rotating left by one makes the second stop the first and sends the first stop to the end.
String first = route[0];
for (int i = 0; i < route.length - 1; i++)
{
route[i] = route[i + 1];
}
route[route.length - 1] = first;
Each pass copies an element one place toward the front, so the copying has to run from the front. Each pass then overwrites a value that's already safe, either saved in first or copied on the pass before. The last statement puts "Depot" at the end, and the route reads "Library", "Market", "Pier", "Stadium", and "Depot".
Rotating right by one runs the other way. The last stop is saved, every element is copied one place toward the back, and the saved stop goes to index 0.
String end = route[route.length - 1];
int k = route.length - 1;
while (k > 0)
{
route[k] = route[k - 1];
k--;
}
route[0] = end;
The route then reads "Stadium", "Depot", "Library", "Market", and "Pier".
In both directions the copying starts at the end the values are traveling toward, so no element is overwritten until it has been copied. A left rotation whose copying starts from the back fails. Its first copy puts "Stadium" at index 3 before "Pier" has gone anywhere, and each later copy repeats it, so the first four elements all become "Stadium".
Shifting
A shift is a rotation without the saved value. If the depot closes, shifting left drops "Depot" from the route: the left rotation's loop runs, with no first to save. Afterward the last element still holds "Stadium", a second copy of it, so a separate statement such as route[route.length - 1] = null; clears it when the extra copy shouldn't stay. The left rotation and the left shift side by side:
Index01234
Rotated leftLibraryMarketPierStadiumDepot
Shifted leftLibraryMarketPierStadiumnull