当前位置:网站首页>集合框架 泛型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);
}
}
边栏推荐
猜你喜欢

Code source AQS sous - jacent pour la programmation simultanée juc

orthofinder直系同源蛋白分析及结果处理

Oracle uses an SQL to find out which data is not in a table

When multiple network devices exist, how to configure their Internet access priority?

MySQL索引详解

JVM常见的垃圾收集器

MySQL锁详解

并发编程JUC的AQS底层源码

How Oracle converts strings to multiple lines

JVM common garbage collector
随机推荐
Lvgl usage demo and instructions 2
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
JVM common garbage collector
Several cases that do not initialize classes
A classic interview question covering 4 hot topics
[original] typescript string UTF-8 encoding and decoding
How Oracle converts strings to multiple lines
IMX8QXP DMA资源和使用(未完结)
orthofinder直系同源蛋白分析及结果处理
数据类型占内存大小?LongVsObject
Coggle 30 days of ML July competition learning
数字IC-1.9 吃透通信协议中状态机的代码编写套路
Read datasets iteratively with xgboost
【原创】TypeScript字符串utf-8编码解码
Getting started with webrtc: 12 Rtendpoint and webrtcendpoint under kurento
The largest rectangle in the bar graph of force buckle 84
0号进程,1号进程,2号进程
分析日志.log
枚举?构造器?面试Demo
sql注入之order by注入