Reversing an ArrayList Example Java Program


Definition

An ArrayList is a non-syncronized class. It implements List Interface and inherits AbstractList class. It maintains insertion order. The Collections class contains a default method 'reverse' to reverse the order of ArrayList.

Syntax

Collections.reverse(arraylist);

Reverse an ArrayList Example Program

import java.util.ArrayList;
import java.util.Collections;

class ReversingArrayList{
	public static void main(String[] args){
		ArrayList<String> arraylist = new ArrayList<String>();
		
		arraylist.add("NORTH");
		arraylist.add("SOUTH");
		arraylist.add("EAST");
		arraylist.add("WEST");
		
		System.out.println("Arraylist is : "+arraylist);//Printing actual arraylist
		Collections.reverse(arraylist);//Reversing order of arraylist
		System.out.println("Reversed arraylist is : "+arraylist);// Printing arraylist after reversing
	}
}

Sample Output

Arraylist is : [NORTH, SOUTH, EAST, WEST]
Reversed arraylist is : [WEST, EAST, SOUTH, NORTH]