当前位置:网站首页>集合框架 泛型LinkedList TreeSet
集合框架 泛型LinkedList TreeSet
2022-06-27 08:45:00 【万伏小太阳】
//package LinkedList;
import java.util.*;
public class TryLinkedlist {
public static void main(String args[ ]) {
List<Student> list = new LinkedList<Student>();
list.add(new Student("张三",86));
list.add(new Student("李四",83));
list.add(new Student("赵大龙",89));
list.add(new Student("李云龙",80));
Iterator<Student> iter=list.iterator();
int sum=0;
System.out.println("排序前,链表中的数据:");
while(iter.hasNext()){
Student stu=iter.next();
System.out.println(stu.name+ "成绩:"+stu.score);
}
System.out.println("排序(按成绩)后,链表中的数据:");
/*完善代码:使用Collection类的sort方法排序并输出——注意格式*/
Collections.sort(list,new Comparator<Student>(){
public int compare(Student o1, Student o2) {
return o1.score-o2.score;
}
});
Iterator<Student> iter2=list.iterator();
while(iter2.hasNext()){
Student stu=iter2.next();
sum+=stu.score;
System.out.println(stu.name+ "成绩:"+stu.score);
}
System.out.println("平均成绩:"+sum/list.size());
/*完善代码:使用Linkedlist类的size方法求平均分——注意格式*/
}
}
class Student implements Comparable {
String name;
int score;
Student(String name,int score) {
this.name=name;
this.score=score;
}
public int compareTo(Object b) {
Student st=(Student)b;
return (this.score-st.score);
}
}
//package TreeSet;
import java.util.*;
public class TryTreeSet{
public static void main(String args[]) {
List<Student> list=new LinkedList<Student>();
int score []={
65,76,45,99,77,88,100,79};
String name[]={
"张三","李四","旺季","加戈","为哈","周和","赵李","将集"};
for(int i=0;i<score.length;i++){
list.add(new Student(score[i],name[i]));
}
Iterator<Student> iter=list.iterator();
TreeSet<Student> mytree=new TreeSet<Student>();
while(iter.hasNext()){
Student stu=iter.next();
mytree.add(stu);
}
/*完善代码:将list中的数据输入到树集中*/
Iterator<Student> te=mytree.iterator();
/*完善代码:输出树集排序结果*/
while(te.hasNext()){
Student stu=te.next();
System.out.println(stu.name+" "+stu.english);
}
}
}
class Student implements Comparable {
int english=0;
String name;
Student(int english,String name) {
this.name=name;
this.english=english;
}
public int compareTo(Object b) {
Student st=(Student)b;
return (this.english-st.english);
}
}
边栏推荐
- 2022.6.26-----leetcode.710
- Game asset reuse: a new way to find required game assets faster
- [ 扩散模型(Diffusion Model) ]
- JS EventListener
- Today's three interviews demo[integer ASCII class relationship]
- sql注入之order by注入
- 支付宝微信支付业务流程图
- 使线程释放锁资源的操作/方法重载一点注意事项
- Correctly understand MySQL mvcc
- [daily practice] realization of product card animation effect
猜你喜欢

RockerMQ消息发送与消费模式

oracle用一条sql查出哪些数据不在某个表里

多网络设备存在时,如何配置其上网优先级?

This, constructor, static, and inter call must be understood!

JVM common garbage collector

Flow chart of Alipay wechat payment business

Rman-08137 main library failed to delete archive file

RMAN-08137 主库无法删除归档文件

即构「畅直播」,全链路升级的一站式直播服务

Understanding mvcc in MySQL transactions is super simple
随机推荐
sql注入之order by注入
看看volatile你深知多少
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
Obsidian 一周使用心得(配置、主题和插件)
力扣84柱状图中最大的矩形
JVM common garbage collector
MySQL index details
【原创】TypeScript字符串utf-8编码解码
ZABBIX deployment instructions (server+win client + switch (H3C))
支付宝微信支付业务流程图
浏览器的markdown插件显示不了图片
VIM from dislike to dependence (19) -- substitution
JVM层次上的对象的创建过程和内存布局
[diffusion model]
Tips for using Jupiter notebook
Read datasets iteratively with xgboost
Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
ServletConfig and ServletContext
即构「畅直播」,全链路升级的一站式直播服务
Redis的事务