Assuming you have gone through arraylist in java and know about arraylist. This post contains different examples for reversing an arraylist which are given below:
1. By writing our own function(Using additional space): reverseArrayList() method in RevArrayList class contains logic for reversing an arraylist with integer objects. This method takes an arraylist as a parameter, traverses in reverse order and adds all the elements to the newly created arraylist. Finally the reversed arraylist is returned.
Elements before reversing:1 2 3 4 Elements after reversing:4 3 2 1
1. By writing our own function(without using additional space): In the previous example, an arraylist is used additionally for storing all the reversed elements which takes more space. To avoid that, same arraylist can be used for reversing.
1. Run the loop for n/2 times where ‘n’ is the number of elements in the arraylist.
2. In the first pass, Swap the first and nth element
3. In the second pass, Swap the second and (n-1)th element and so on till you reach the mid of the arraylist.
4. Return the arraylist after the loop termination.
Elements before reversing: 12 13 123 54 1 Elements after reversing: 1 54 123 13 12
2. By using Collections class: Collections is a class in java.util package which contains various static methods for searching, sorting, reversing, finding max, min….etc. We can make use of the In-built Collections.reverse() method for reversing an arraylist. It takes a list as an input parameter and returns the reversed list.
Elements before reversing: 9 145 878 343 5 Elements after reversing: 5 343 878 145 9
3. Reversing an arraylist of user defined objects: An Employee class is created for creating user defined objects with employeeID, employeeName, departmentName as class variables which are initialized in the constructor. An arraylist is created that takes only Employee(user defined) Objects. These objects are added to the arraylist using add() method. The arraylist is reversed using In-built reverse() method of Collections class.
The printElements() static method is used only to avoid writing one more class in the program.
Elements before reversing: EmpID:123, EmpName:Rama, Department:Facilities EmpID:124, EmpName:Lakshman, Department:Transport EmpID:125, EmpName:Ravan, Department:Packing Elements after reversing: EmpID:125, EmpName:Ravan, Department:Packing EmpID:124, EmpName:Lakshman, Department:Transport EmpID:123, EmpName:Rama, Department:Facilities
- ArrayList of ArrayList in Java
- Java.util.ArrayList.addall() method in Java
- ArrayList in Java
- Arraylist.contains() in Java
- Java.util.Arraylist.indexOf() in Java
- Java.util.ArrayList.add() Method in Java
- How to get ArrayList from Stream in Java 8
- Get first and last elements from ArrayList in Java
- ArrayList vs LinkedList in Java
- ArrayList vs. HashMap in Java
- Synchronization of ArrayList in Java
- Vector vs ArrayList in Java
- Arraylist lastIndexOf() in Java with example
- Array vs ArrayList in Java
- Array of ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SujanM