How do I reverse array elements order?

In this example we are going to use the ArraysUtils helper class from Apache Commons Lang library to reverse the order of array elements. The method to reverse the order of array elements is ArrayUtils.reverse() method.

The ArrayUtils.reverse() method is overloaded so we can reverse other types of array such as java.lang.Object, long, int, short, char, byte, double, float and boolean.

package org.kodejava.example.commons.lang;

import org.apache.commons.lang.ArrayUtils;

public class ArrayReverseExample {

    public static void main(String[] args) {
        // Define colors array.
        String[] colors = {"Red", "Green", "Blue", "Cyan", "Yellow", "Magenta"};

        // Now we reverse the order of array elements. 

Here is the output of the code snippet above:


Maven Dependencies

<!-- -->


Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. Support me by donating.

Leave a Reply