Monday, March 16, 2015

To reverse an array list in java pgm



To reverse an array list
 
import java.util.ArrayList;
import java.util.Collections;
public class Main
 {
   public static void main(String[] args) 
{
      ArrayList arrayList = new ArrayList();
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
      arrayList.add("D");
      arrayList.add("E");
      System.out.println("Before Reverse Order: " + arrayList);
      Collections.reverse(arrayList);
      System.out.println("After Reverse Order: " + arrayList);
   }
}

Result:

Before Reverse Order: [A, B, C, D, E]
After Reverse Order: [E, D, C, B, A]

No comments:

Post a Comment