【练手】用程序给出随便大小的10个数,序号为1-10,按从小到大顺序输出,并输出相应的序号。

来源:互联网 发布:舒尔特软件下载 编辑:程序博客网 时间:2024/06/10 03:28

既然是对应关系,那就用map来做,既然数字要排序,序号还要对应,逆向思维,把序号作为key,数作为值。

import java.util.*;//用程序给出随便大小的10个数,序号为1-10,按从小到大顺序输出,并输出相应的序号。public class TenNumber{public static void main(String[] args) {//我举得用map做容易点,正好对map不熟悉,借此来熟悉一下map的方法Map<Integer,Integer> m=new LinkedHashMap<Integer,Integer>();Random r=new Random();for(int i=0;i<10;i++){m.put(r.nextInt(1000),i);}System.out.println(m);ArrayList<Integer> sortedList=new ArrayList<Integer>(m.keySet());Collections.sort(sortedList);System.out.println(sortedList);for(int i:sortedList){System.out.print("random Number is  "+i);System.out.println("  order is  "+m.get(i));}}}

原创粉丝点击