20165301第十周课下补做
第十周课下补做
知识点总结
-
创建一个空的链表
List<Student> list = new LinkedList<Student>();
-
向链表中添加新的结点
list.add(new Student(XXXXXXXXXXXXXXX));
-
删除结点
list.remove("xxxxxxx");
-
链表中数据的插入
list.add("**");
-
链表中数据的排序
Collections.sort();
-
将list中的元素按升序排序
public static sort(List<E>list)
习题2
代码
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[] = {"20165301陈潭飞", "20165302程上杰", "20165338胡麟", "20165303魏煜", "20165339唐羽童"};
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("20165305");
list.add("20165302");
list.add("20165303");
list.add("20165304");
System.out.println("打印初始链表");
//把上面四个节点连成一个没有头结点的单链表
Iterator<String> iter=list.iterator();
while(iter.hasNext()){
String te=iter.next();
System.out.println(te);
}
//遍历单链表,打印每个结点的
list.add("20165301");
//把你自己插入到合适的位置(学号升序)
System.out.println("插入我的学号后排序,打印链表");
Collections.sort(list);
iter=list.iterator();
while(iter.hasNext()){
String te=iter.next();
System.out.println(te);
}
//遍历单链表,打印每个结点的
list.remove("20165301");
//从链表中删除自己
System.out.println("删除我的学号后打印链表");
iter=list.iterator();
while(iter.hasNext()){
String te=iter.next();
System.out.println(te);
}
//遍历单链表,打印每个结点的
}
}
实验截图
教材课后作业
- 使用堆栈结构输出an的若干项,其中an=2an-1+2an-2,a1=3,a2=8.
- 将链表中的学生英语成绩单存放到一个树集中,使得按成绩自动排序,并输出排序结果
- 有10个U盘,有两个重要的属性:价格和容量,编写一个应用程序,使用TreeMap