standard-library-in-x

Notes and readings for STL workshop

View on GitHub

reverse()

Function

It reverses the order of the elements in the specified list.

This method can be used to reverse a LinkedList, ArrayList and arrays.

Declaration

public static void reverse(List<?> list)	

Parameters

list - The list which is to be sorted.

Example

import java.util.*;

public class ReverseList 
{
	public static void main(String[] args) 
	{
      		ArrayList<String> letters = new ArrayList<String>();
		letters.add("A");
		letters.add("B");
		letters.add("C");
		letters.add("D");
		letters.add("E");

		System.out.println("Before reversing: "+letters);
		Collections.reverse(letters);
		System.out.println("After reversing: "+letters);
   }
}

Output

Before reversing: [A, B, C, D, E]
After reversing: [E, D, C, B, A]