/home/wpollock1/public_html/Java/ArrayDemo.java

// Demo to reverse an array of Strings in-place (without using a
// second array); an idea taken from
// <https://www.java67.com/2016/01/java-program-to-reverse-array-in-place.html>.
// Written 10/2020 by Wayne Pollock, Tampa Florida USA

import java.util.Arrays;

class ArrayDemo {
    public static void main ( String[] args ) {
        String[] data = { "Moe", "Larry", "Curly", "Shemp" };
        System.out.println( "Original Array:" );
        System.out.println( Arrays.toString(data) );
        reverse( data );
        System.out.println( "Reversed Array:" );
        System.out.println( Arrays.toString(data) );
    }

    // Swap the end elements, then the second and second-to-end, etc.,
    // working toward the middle.  (Note a recursive solution might
    // be simpler.
    public static void reverse(String[] array) {
        if (array == null || array.length < 2) {
            return;
        }
        int len = array.length;
        for (int i = 0; i < len / 2; i++) {
            // Swap the i-th and the i-th-from-the-end elements:
            String temp = array[i];
            array[i] = array[len - 1 - i];
            array[len - 1 - i] = temp;
        }
    }
}