代码
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);
}
}
}
截图