当前位置:网站首页>集合框架 泛型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);
}
}
边栏推荐
- Flow chart of Alipay wechat payment business
- SPARQL基础入门练习
- 内存泄露的最直接表现
- webrtc入门:12.Kurento下的RtpEndpoint和WebrtcEndpoint
- MySQL lock details
- Creation process and memory layout of objects at JVM level
- CLassLoader
- Chapter 11 signal (I) - concept
- 我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
- Imx8qxp DMA resources and usage (unfinished)
猜你喜欢
Matlab tips (19) matrix analysis -- principal component analysis
Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
2022.06.26(LC_6101_判断矩阵是否是一个 X 矩阵)
orthofinder直系同源蛋白分析及结果处理
Matlab tips (18) matrix analysis -- entropy weight method
How Oracle converts strings to multiple lines
Redis的事务
JVM层次上的对象的创建过程和内存布局
One week's experience of using Obsidian (configuration, theme and plug-in)
正确的理解MySQL的MVCC
随机推荐
C# 解决使用SQLite 的相对路径问题
The background prompt module for accessing fastadmin after installation does not exist
Some considerations on operation / method overloading for thread to release lock resources
The difference between ArrayList and LinkedList
DataV轮播表组件dv-scroll-board宽度问题
快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])
Redis master-slave replication and sentinel mode
IO管脚配置和pinctrl驱动
fastadmin 安装后访问后台提示模块不存在
A classic interview question covering 4 hot topics
[diffusion model]
Object contains copy method?
Five basic types of redis
ServletConfig and ServletContext
C # solve the relative path problem using SQLite
MySQL锁详解
【云原生】2.3 Kubernetes 核心实战(上)
使线程释放锁资源的操作/方法重载一点注意事项
Redis transactions
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!