Thursday, December 12, 2013

Reverse, Shuffle and Sort List

Example:
package java_reverselist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @web java-buddy.blogspot.com
 */
public class Java_ReverseList {

    @SuppressWarnings("empty-statement")
    public static void main(String[] args) {
        
        List<String> list = 
                Arrays.asList("A", "B", "C", "D", 
                        "E", "F", "G", "H", "I", "J");
        
        System.out.println("original list:");
        for(String s: list){
            System.out.print(s);
        }
        
        Collections.reverse(list);
        System.out.println("\nreversed list:");
        for(String s: list){
            System.out.print(s);
        }

        Collections.shuffle(list);
        System.out.println("\nshuffled list:");
        for(String s: list){
            System.out.print(s);
        }
        
        Collections.sort(list);
        System.out.println("\nsorted list:");
        for(String s: list){
            System.out.print(s);
        }

        System.out.println("\n");
    }
    
}


Reverse, Shuffle and Sort List
Reverse, Shuffle and Sort List

No comments:

Post a Comment