public static void test1(){
Integer[] s = {1,3,5,7,9,11,13,15,17,19};
Comparator cmp = new MyComparator();
Arrays.sort(s, cmp);
for(Integer i:s){
System.out.println(i);
}
}
static class MyComparator implements Comparator
@Override
public int compare(Integer o1, Integer o2) {
return o2 -o1;
}
}
public static void test2(){
try{
Scanner scan = new Scanner(System.in);
System.out.println("请输入学生数:");
int count = scan.nextInt();
int cur = 0;
List
while(cur System.out.println("请输入第" + (cur+1) + "个学生的姓名"); String name = scan.next(); System.out.println("请输入第" + (cur+1) + "个学生的成绩"); float sco = scan.nextFloat(); Map m.put("name", name); m.put("score", sco); list.add(m); cur++; } Collections.sort(list, new Comparator @Override public int compare(Map if (o1.get("score") == null && o2.get("score") == null) return 0; if (o1.get("score") == null) return -1; if (o2.get("score") == null) return 1; return Float.valueOf(o2.get("score").toString()).compareTo(Float.valueOf(o1.get("score").toString())); } }); for(Map System.out.println("学生姓名:"+m.get("name")+",成绩:"+m.get("score")); } }catch(Exception e){ e.printStackTrace(); } } public static void test3(){ Integer[] a1 = {1,3,5,7,9,1,1,15,17,19}; Integer[] a2 = new Integer[a1.length]; for(int i=0;i a2[i] = a1[i]; } Arrays.sort(a1); Integer min = a1[0]; for(int i=0;i if(min==a2[i]){ System.out.println("最小元素下标:"+i); } } } public static void test4(){ Integer[] a1 = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; List for(int i=0;i if(0!=a1[i]){ list.add(a1[i]); } } Integer[] a2 = new Integer[list.size()];//新数组 for(int i=0;i a2[i] = list.get(i); } for(Integer s :a2){ System.out.print(s+" "); } } public static void test5(){ Integer[] a1 = {1,7,9,11,13,15,17,19}; Integer[] a2 = {2,4,6,8,10}; List for(Integer i:a1){ list.add(i);// // if(!list.contains(i)){//去重复处理,需要再加 // // } } for(Integer i:a2){ list.add(i);// } Integer[] a3 = new Integer[list.size()];//新数组 for(int i=0;i a3[i] = list.get(i); } Arrays.sort(a3); }