Reverse of an array in java?

Reverse of an array in java?

Previous Home Next

 

This program shows how to reverse the order of elements of an array.

Reverse method of collection class is used in this program to reverse the elements of an array list.	
 static void reverse(List list) method of Collections class is used in this program .
This method reverse the order of elements of specified list.

 Collections.reverse(arrayList);
 this method is reverse the order of the elements of the array list.

 


import java.util.ArrayList;
import java.util.Collections;
 
public class ArrayListExample {
 
  public static void main(String[] args) {
  ArrayList arrayList = new ArrayList();
 
    //Add elements to Arraylist
    arrayList.add("10");
    arrayList.add("20");
    arrayList.add("30");
    arrayList.add("40");
    arrayList.add("50");
  System.out.println("Before Reverse Order, ArrayList Contains : " + arrayList);
  Collections.reverse(arrayList);
 
    System.out.println("After Reverse Order, ArrayList Contains : " + arrayList);
 
  }

Before Reverse Order ArrayList Contains: 10 20 30 40 50
After Reverse Order ArrayList Contains :50 40 30 20 10 
Previous Home Next