javaSE集合框架—— 练习题

Posted 玛丽莲茼蒿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaSE集合框架—— 练习题相关的知识,希望对你有一定的参考价值。

1. 请把学生名与考试分数录入到集合中,并按分数显示前三名成绩学院的名字

法(一):利用TreeSet结构+自然排序

注意01 :CompareTo方法里还要对name进行比较,防止score一样的添加不进去。并且应该“先比较score,成绩相同再比较name”

    @Test
    public void test01()
        TreeSet treeSet = new TreeSet();
        Student aa = new Student("AA", 98);
        Student bb = new Student("BB", 98);  //注意CompareTo方法里还要对name进行比较,防止score一样的添加不进去
        Student cc = new Student("CC", 77);
        Student dd = new Student("DD", 92);
        Student ee = new Student("EE", 80);

        treeSet.add(aa);
        treeSet.add(bb);
        treeSet.add(cc);
        treeSet.add(dd);
        treeSet.add(ee);

        int i=0;
        for(Object obj:treeSet)
            if(i==3) break;
            Student stu =(Student)obj;
            System.out.println(stu.getName());
            i++;
        
        /*
        * 输出的结果:
        * AA
        * BB
        * DD
        * */
    
package Collections;

public class Student implements Comparable
    private String name;
    private int score;

    public Student()

    

    public Student(String name, int score)
        this.name=name;
        this.score=score;
    

    public String getName()
        return this.name;
    

    @Override
    public int compareTo(Object o) 
        if(o instanceof Student)
            Student user =(Student) o;
            int scoreCompare = -(this.score - user.score);
            if(scoreCompare!=0)  //分数不相同,OK
                return scoreCompare;
            else //分数相同,还要保证名字不相同
                return this.name.compareTo(user.name);
        else
            throw new RuntimeException("输出的类型不匹配");
        
    


法(二):利用ArrayList结构+定制排序

因为ArrayList可以存储重复的元素,所以只需要比较score就行了,不用再去比较name。

    @Test
    public void test02()
        Comparator com = new Comparator() 
            @Override
            public int compare(Object o1, Object o2) 
                if(o1 instanceof Student && o2 instanceof Student)
                    Student stu1 = (Student)o1;
                    Student stu2 = (Student)o2;
//                    int scoreCompare = -(stu1.getScore()-stu2.getScore());
//                    if(scoreCompare!=0)
//                        return scoreCompare;
//                    else
//                        return stu1.getName().compareTo(stu2.getName());
//                    
                    return -(stu1.getScore()-stu2.getScore());
                else
                    throw new RuntimeException("输出的数据类型不匹配");
                
            
        ;

        List list = new ArrayList();

        Student aa = new Student("AA", 98);
        Student bb = new Student("BB", 98);
        Student cc = new Student("CC", 77);
        Student dd = new Student("DD", 92);
        Student ee = new Student("EE", 80);

        list.add(aa);
        list.add(bb);
        list.add(cc);
        list.add(dd);
        list.add(ee);

        Collections.sort(list, com);
        for(int i=0; i<3 ; i++)
            Student stu =(Student)list.get(i);
            System.out.println(stu.getName());
        
        /*
        * 输出的结果:
        * AA
        * BB
        * DD
        * */
    
public class Student implements Comparable
    private String name;
    private int score;

    public Student()

    

    public Student(String name, int score)
        this.name=name;
        this.score=score;
    

    public String getName()
        return this.name;
    

    public int getScore()
        return this.score;
    


2.

 3. 

以上是关于javaSE集合框架—— 练习题的主要内容,如果未能解决你的问题,请参考以下文章

JavaSE-19.1.1IO流练习案例-集合到文件(排序改进版)

[javaSE] 集合框架(迭代器)

JavaSE-16 集合框架

JAVASE02-Unit04: 集合框架 集合操作 —— 线性表

JavaSE——集合框架

JavaSE入门学习40:Java集合框架之泛型