Reverse a list

ReturnMethodSummary
static void reverse(List<?> list) Reverses the order of the elements in the specified list.

  import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Main {

  public static void main(String args[]) {

    List<Character> ll = new LinkedList<Character>();

    for (char n = 'A'; n <= 'F'; n++)
      ll.add(n);

    System.out.println("Here is the original list: ");
    for (Character x : ll)
      System.out.print(x + " ");
    Collections.reverse(ll);

    System.out.println("Here is the reversed list: ");
    for (Character x : ll)
      System.out.print(x + " ");


  }
}
  

The output:


Here is the original list: 
A B C D E F Here is the reversed list: 
F E D C B A
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.