September 2019
Intermediate to advanced
816 pages
18h 47m
English
There are several solutions to this problem. Some of them mutate the initial array, while others just return a new array.
Let's assume the following array of integers:
int[] integers = {-1, 2, 3, 1, 4, 5, 3, 2, 22};
Let's start with a simple implementation that swaps the first element of the array with the last element, the second element with the penultimate element, and so on:
public static void reverse(int[] arr) { for (int leftHead = 0, rightHead = arr.length - 1; leftHead < rightHead; leftHead++, rightHead--) { int elem = arr[leftHead]; arr[leftHead] = arr[rightHead]; arr[rightHead] = elem; }}
The preceding solution mutates the given array and this is not always the desired behavior. Of course, we can modify ...