教科書P448 Example15_4

タスクの詳細

教科書P448 Example15_4

  1. 学校の番号の後に3人の学生の独自のリストを追加し、学生の数が参加する最初から最後の3第1号であります
  2. 業績提出スクリーンショット
  3. クラウド刻まれたコードにコードをプッシュ

コード

import java.util.*;
class Student implements Comparable {
    int height=0;
    String name;
    Student(String n,int h) {
        name=n;
        height = h;

    }
    public int compareTo(Object b) { // 两个Student对象相等当且仅当二者的height值相等
        Student st=(Student)b;
        return (this.height-st.height);
    }
}
public class Example15_4 {
    public static void main(String args[ ]) {
        List<Student> list = new LinkedList<Student>();
        list.add(new Student("张三",188));
        list.add(new Student("李四",178));
        list.add(new Student("周五",198));
        list.add(new Student("5336",181));
        list.add(new Student("5337",176));
        list.add(new Student("5338",171));
        Iterator<Student> iter=list.iterator();
        System.out.println("排序前,链表中的数据");
        while(iter.hasNext()){
            Student stu=iter.next();
            System.out.println(stu.name+ "身高:"+stu.height);
        }
        Collections.sort(list);
        System.out.println("排序后,链表中的数据");
        iter=list.iterator();
        while(iter.hasNext()){
            Student stu=iter.next();
            System.out.println(stu.name+ "身高:"+stu.height);
        }
        Student zhaoLin = new Student("zhao xiao lin",178);
        int index = Collections.binarySearch(list,zhaoLin,null);
        if(index>=0) {
            System.out.println(zhaoLin.name+"和链表中"+list.get(index).name+"身高相同");
        }
    }
}

実行ショット

コードリンク

https://gitee.com/yukungbhy/yukun-20155335/commit/6fd125be2f5091f6230dc350befcbf878421c58f

おすすめ

転載: www.cnblogs.com/yukungbhy/p/10973860.html