List reversing
In this chapter you will learn:
Reverse a list
static void reverse(List<?> list)
from Collections
class
Reverses the order of the elements in the specified list.
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/* j a va 2s .c o m*/
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:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » List