20165332第十周课下作业
知识点总结
创建一个空的链表
List
向链表中添加新的结点
list.add(new Student(XXXXXXXXXXXXXXX));
删除结点
list.remove("xxxxxxx");
链表中数据的插入
list.add("**");
链表中数据的排序
Collections.sort();
将list中的元素按升序排序
public static sort(List
习题二
代码:
import java.util.*;
class StudentSort implements Comparable {
double d=0;
String s="";
StudentSort (double d) {
this.d=d;
}
StudentSort (String s) {
this.s=s;
}
public int compareTo(Object b) {
StudentSort st=(StudentSort)b;
if((this.d-st.d)==0)
return -1;
else
return (int)((this.d-st.d)*1000);
}
}
class Student {
String name=null;
double math,english,computer,total,aver;
Student(String s, double m, double e, double f, double a,double b) {
name=s;
math=m;
english=e;
computer=f;
total=a;
aver=b;
}
}
public class paixu {
public static void main(String args[]) {
TreeMap<StudentSort, Student> treemap = new TreeMap<StudentSort, Student>();
String str[] = {"20165332延亿卓", "201653张羽昕", "20165331胡麟", "20165333陈国超", "20165334李天龙"};
double math[] = {89, 48, 74, 76, 70};
double english[] = {62, 69, 69, 70, 71};
double computer[] = {99, 66, 54, 75, 80};
double total[] = new double[5];
double aver[] = new double[5];
Student student[] = new Student[5];
for (int k = 0; k < student.length; k++) {
total[k] = math[k] + english[k] + computer[k];
aver[k] = total[k] / 3;
}
for (int k = 0; k < student.length; k++) {
student[k] = new Student(str[k], math[k], english[k], computer[k], total[k], aver[k]);
}
StudentSort key[] = new StudentSort[5];
for (int k = 0; k < key.length; k++) {
key[k] = new StudentSort(student[k].total);
}
for (int k = 0; k < student.length; k++) {
treemap.put(key[k], student[k]);
}
int number = treemap.size();
System.out.println("有" + number + "个对象,按总成绩排序:");
Collection<Student> collection = treemap.values();
Iterator<Student> iter = collection.iterator();
while (iter.hasNext()) {
Student stu = iter.next();
System.out.println("姓名 " + stu.name + " 总成绩 " + stu.total);
}
}
}
截图:
习题三:
代码:
import java.util.*;
public class Mylist {
public static void main(String [] args) {
List<String> list=new LinkedList<String>();
list.add("20165333");
list.add("20165334");
list.add("20165335");
list.add("20165336");
System.out.println("打印初始链表");
//把上面四个节点连成一个没有头结点的单链表
Iterator<String> iter=list.iterator();
while(iter.hasNext()){
String te=iter.next();
System.out.println(te);
}
//遍历单链表,打印每个结点的
list.add("20165332");
//把你自己插入到合适的位置(学号升序)
System.out.println("插入我的学号后排序,打印链表");
Collections.sort(list);
iter=list.iterator();
while(iter.hasNext()){
String te=iter.next();
System.out.println(te);
}
//遍历单链表,打印每个结点的
list.remove("20165332");
//从链表中删除自己
System.out.println("删除我的学号后打印链表");
iter=list.iterator();
while(iter.hasNext()){
String te=iter.next();
System.out.println(te);
}
//遍历单链表,打印每个结点的
}
}
截图:
原文地址:https://www.cnblogs.com/yyzzuishuai/p/9000207.html
时间: 2024-11-29 03:22:56