Reverse an Array

Author: Tatyana Milkina

1. Reversing an Array in Java

In this lesson, we'll examine how to reverse an array in Java. Reversing (or inverting) means flipping the order of elements in an array.

Suppose we have an array: {1, 2, 3, 4}. After reversing, it should become: {4, 3, 2, 1}.

We calculate the middle index as array.length / 2. Then, using a loop, we swap elements symmetrically — the one at position i with the one at array.length - i - 1. We use a temporary variable for swapping:

public class ArrayInverter {
    public static void invert(int[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            int tmp = array[i];
            array[i] = array[array.length - i - 1];
            array[array.length - i - 1] = tmp;
        }
    }
}

2. Calling invert() from Another Class

Note that the class ArrayInverter does not contain a main() method. To test it, we'll call invert() from another class using: ArrayInverter.invert(array).

import java.util.Arrays;

public class ArrayInverterExample1 {
    public static void main(String[] args) {
        int[] array1 = new int[]{};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));

        array1 = new int[]{0};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));

        array1 = new int[]{0, 1};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));

        array1 = new int[]{0, 1, 2};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));

        array1 = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));
    }
}

3. Improving the Code: Remove Repetition

As we can see in ArrayInverterExample1, the block of code that prints the array before and after reversing is repeated multiple times. Repetition is considered bad practice in software development.

Let’s extract the repeated logic into a separate method testInvert(), which we’ll call with different input arrays:

import java.util.Arrays;

public class ArrayReverseExample {
    public static void main(String[] args) {
        testInvert(new int[]{});
        testInvert(new int[]{0});
        testInvert(new int[]{0, 1});
        testInvert(new int[]{0, 1, 2, 3, 4});
    }

    private static void testInvert(int[] array) {
        System.out.print(Arrays.toString(array) + " => ");
        ArrayInverter.invert(array);
        System.out.println(Arrays.toString(array));
    }
}

Conclusion

Reversing an array in Java is a simple and essential technique that helps understand array manipulation, indexing, and loop logic. For performance-critical code, prefer in-place reversal with swapping, as shown above.

Курс 'Java для начинающих' на Udemy Курс 'Java для начинающих' на Udemy
Read also:
Comments