Reverse an Array

Reversing an array. I can’t really remember the last time I needed to do this, but one thing is for sure, if I did, I didn’t write an ad hoc piece of code to do this: I probably used an ArrayList to do the trick, or some third-party library. But for the sake of education, let’s have a quick look at different solutions.

The Classic way

This method is a classic of algorithm classes: use a temporary value to swap two indexes in the array:

public class NumberReversal {

	public static void reverseArray(int[] array) {
		if (array == null) {
			return;
		}
		int i = 0;
		int j = array.length - 1;
		int tmp;
		while (j > i) {
			tmp = array[j];
			array[j] = array[i];
			array[i] = tmp;
			j--;
			i++;
		}
	}

	public static void main(String[] args) {
		int[] test = { 1, 2, 3, 4 };
		reverseArray(test);
		for (int i : test) {
			System.out.println(i);
		}
	}
}

Recursion

Recursions make CS teachers happy, so here is a solution that uses one. I personally find it hard to read, and recursions, despite being teachers’ pet, are usually frowned upon in real life. It relies on 2 key methods: System.arraycopy, which copies an array into another, and Arrays.copyOfRange. The 2 methods do pretty much the same thing, except Arrays.copyOfRange does some bond checking… and uses System.arraycopy. I used them both so that you are aware of them.

import java.util.Arrays;

public class NumberReversal {

	static int[] concat(int[] a, int[] b ) {
		int[] c = new int[a.length + b.length];
		System.arraycopy(a, 0, c, 0, a.length);
		System.arraycopy(b, 0, c, a.length, b.length);
		return c;
	}

	public static int[] reverseArray(int[] array) {
		if (array.length == 0) {
			return new int[] {};
		} else if (array.length == 1) {
			return array;
		} else {
			// recursion: concatenate the reverse of the end of the array 
			// to the first element (put at the end)
			return concat(
					reverseArray(Arrays.copyOfRange(array, 1, array.length)),
					new int[] { array[0] }
			);
		}
	}

	public static void main(String[] args) {
		int[] test = {1, 2, 3, 4};
		int[] reversed = reverseArray(test);
		for (int i: reversed) {
			System.out.println(i);
		}
	}
}

Using Collections

This solution forces you to deal with the int[] to Integer[] conversion, which is a bit of a pain. So this solution actually cheats by only using Integer objects. But if you wanted to use int[], you would have to do the conversion either “manually”, or using Apache common lang’s ArrayUtils.toPrimitive and ArrayUtils.toObject

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class NumberReversal {

	public static void reverseArray(Integer[] array) {
		List list = Arrays.asList(array);
		Collections.reverse(list);

		array = (Integer[])list.toArray(new Integer[array.length]);
	}

	public static void main(String[] args) {
		Integer[] test = { 1, 2, 3, 4 };
		reverseArray(test);
		for (int i : test) {
			System.out.println(i);
		}
	}
}

The Sensible Solution

This one uses the Apache commons lang library which, a few days ago I was mentioning as one of my favourite libs.

import org.apache.commons.lang.ArrayUtils;

public class NumberReversal {
	public static void main(String[] args) {
		int[] test = {1, 2, 3, 4};
		ArrayUtils.reverse(test);
		for (int i: test) {
			System.out.println(i);
		}
	}
}
 
---

Comment

your_ip_is_blacklisted_by sbl.spamhaus.org

---