Java 用程序给出随便大小的10 个数,序号为1-10,按从小到大顺序输出,并输出相应的序号?

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class RandomSort {
    public static void printRandomBySort() {
        Random random = new Random(); // 创建随机数生成器
        List list = new ArrayList();
        // 生成10 个随机数,并放在集合list 中
        for (int i = 0; i < 10; i++) {
            list.add(random.nextInt(1000));
        }
        Collections.sort(list); // 对集合中的元素进行排序
        Iterator it = list.iterator();
        int count = 0;
        while (it.hasNext()) { // 顺序输出排序后集合中的元素
            System.out.println(++count + ": " + it.next());
        }
    }

    public static void main(String[] args) {
        printRandomBySort();
    }
}

 

posted @ 2016-12-15 09:08  随行-LV  阅读(4687)  评论(0编辑  收藏  举报