20165314课下作业补做

代码

import java.util.*;
class Stu implements Comparable{
    int id;
    String name;
    Stu(String n, int i){
        name=n;
        id=i;
    }
    public int compareTo(Object b){
        Stu st=(Stu)b;
        return (this.id-st.id);
    }
}
public class MyList {
    public static void main(String [] args) {
        LinkedList<Stu> list=new LinkedList<>();
        list.add(new Stu("曹歌",5312));
        list.add(new Stu("张晨晖",5313));
        list.add(new Stu("阎含",5315));
        list.add(new Stu("孙勖哲",5316));
        Iterator<Stu> iter=list.iterator();
        System.out.println("初始单链表为:");
        while (iter.hasNext()){
            Stu st=iter.next();
            System.out.println(st.id+" "+st.name);
        }
        list.add(new Stu("陈由章",5314));
        Collections.sort(list);
        iter=list.iterator();
        System.out.println("插入学号和姓名以后:");
        while (iter.hasNext()){
            Stu st=iter.next();
            System.out.println(st.id+" "+st.name);
        }
        list.remove(1);
        iter=list.iterator();
        System.out.println("删除学号和姓名以后:");
        while (iter.hasNext()){
            Stu st=iter.next();
            System.out.println(st.id+" "+st.name);
        }

    }
}

截图

posted @ 2018-05-06 22:07  飞翔的章帅  阅读(99)  评论(0编辑  收藏  举报