Java program to reverse an ArrayList

Java program to reverse an ArrayList

Write a Java program to reverse an ArrayList is one of the commonly asked Java interview questions. You can reverse an ArrayList in Java by simply using the reverse() method of java.util.Collections class. However the interviewer may ask you to write the program without using the in-built API. In such case you need to write your own implementation. In this post, we will first see Java code using reverse() method and then we will see Java program to reverse order of elements of an ArrayList using loop.

Java program to reverse an ArrayList using Collection’s reverse method

Below is the output of above program.

Java program to reverse an ArrayList using for loop

Below is the output of above program.

Java program to reverse an ArrayList using ListIterator

Below is the output of above program.
When you check the code for the Collections.reverse() method, you will find that the above two implementation will closely resemble the implementation of the reverse() method.

That’s all about Java program to reverse an ArrayList. Let us know if you know any other way to reverse an ArrayList in Java .

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.