课下作业
相关知识点的总结
- LinkedList泛型类
mylist.add("String")
添加节点,这样的节点是自动连在一起的,无需操作安排节点中的所存放的下一个或上一个节点的引用;public void add(int index,E element)
向链表的指定位置添加一个新结点public E remove(int index)
删除指定位置上的节点- 迭代器
Iterator iter=list.iterator();
while(iter.hasNext()){
Student stu=iter.next();
}
- 堆栈:
public E push(E item);
压栈操作,public E pop()
弹栈操作
课上内容补做
任务二
针对下面的Student类,使用Comparator编程完成以下功能:
- 在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后)
- 对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码
import java.util.*;
class Test {
public static void main(String[] args) {
List list = new LinkedList<>();
list.add(new Student(20165206,"谭笑","male",19,86,88,89));
list.add(new Student(20165207,"李天林","male",20,89,80,90));
list.add(new Student(20165208,"孔月","male",19,95,97,91));
list.add(new Student(20165209,"陈思兵","male",20,79,81,92));
list.add(new Student(20165210,"刘宇坤","male",20,72,93,80));
System.out.println("排序前的数据:");
for (Student student : list) {
System.out.println(student);
}
SortByID sortByID = new SortByID();
Collections.sort(list, sortByID);
System.out.println("根据学号由低到高排序:");
for (Student student : list) {
System.out.println(student);
}
SortByTotal_score sortBytotal_score = new SortByTotal_score();
Collections.sort(list, sortBytotal_score);
System.out.println("根据学生成绩由低到高排序:");
for (Student student : list) {
System.out.println(student);
}
}
}
class Student {
private int id;
private String name;
private int age;
private String sex;
private double computer_score;
private double english_score;
private double maths_score;
private double total_score;
private double ave_score;
@Override
public String toString() {
return "Student[name:"+name+",age:"+age+",number:"+id+",total_score"+total_score+"]";
}
public Student(int id, String name, String sex, int age,double computer_score,
double english_score,double maths_score) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.computer_score = computer_score;
this.english_score = english_score;
this.maths_score = maths_score;
}
public int getId() {
return id;
}//获得当前对象的学号,
public double getComputer_score() {
return computer_score;
}//获得当前对象的计算机课程成绩,
public double getMaths_score() {
return maths_score;
}//获得当前对象的数学课程成绩,
public double getEnglish_score() {
return english_score;
}//获得当前对象的英语课程成绩,
public void setId(int id) {
this.id = id;
}// 设置当前对象的id值,
public void setComputer_score(double computer_score) {
this.computer_score = computer_score;
}//设置当前对象的Computer_score值,
public void setEnglish_score(double english_score) {
this.english_score = english_score;
}//设置当前对象的English_score值,
public void setMaths_score(double maths_score) {
this.maths_score = maths_score;
}//设置当前对象的Maths_score值,
public double getTotalScore() {
total_score=computer_score + maths_score + english_score;
return total_score;
}// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。
public double getAveScore() {
return getTotalScore() / 3;
}// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。
}
class SortByID implements Comparator {
@Override
public int compare(Student o1, Student o2) {
return o1.getId() - o2.getId();
}
}
class SortByTotal_score implements Comparator {
@Override
public int compare(Student o1, Student o2) {
return (int)( o1.getTotalScore() - o2.getTotalScore());
}
}
任务三
参见附件,补充MyList.java的内容
import java.util.*;
public class MyList {
public static void main(String [] args) {
LinkedList mylist=new LinkedList();
mylist.add("20165206");
mylist.add("20165207");
mylist.add("20165209");
mylist.add("20165210");
for(int i=0;i<4;i++){
String temp=(String)mylist.get(i);
System.out.println("第"+i+"节点中的数据:"+temp);
}
mylist.add(2,"20165208");
for(int i=0;i<5;i++){
String temp=(String)mylist.get(i);
System.out.println("第"+i+"节点中的数据:"+temp);
}
mylist.remove("20165208");
for(int i=0;i<4;i++){
String temp=(String)mylist.get(i);
System.out.println("第"+i+"节点中的数据:"+temp);
}
}
}
问题反思
本次测试出现的问题一是还不够熟练,在任务二时只匆匆完成了一步,没有完成试验要求;二是在使用方法 mylist.add(2,20165208)
时错把关键词的顺序写错,正确输入应为前者为指定位置后者为指定数据;三是在课下进行修改时发现直接利用mylist.remove(2)
无法实现实验目的,他会将后续所有结点一并删除。除此之外还有一个错误是未将关键词用“”
引上,导致输入失败,而无法显示第二步的插入。
补做教材第十五章的编程题目
- 使用堆栈结构输出an的若干项,其中an=2an-1+2an-2,a1=3,a2=8
import java.util.*;
public class Ex {
public static void main(String args[]) {
Stack stack=new Stack();
stack.push(new Integer(3));
stack.push(new Integer(8));
int k=1;
while(k<=8) {
Integer F1=stack.pop();
int f1=F1.intValue();
Integer F2=stack.pop();
int f2=F2.intValue();
Integer temp=new Integer(2*f1+2*f2);
System.out.println(""+temp.toString());
stack.push(temp);
stack.push(F2);
k++;
}
}
}
- 将链表中的学生英语成绩单存放到一个树集中,使得按成绩自动排序,并输出排序结果
import java.util.*;
class CollegeStu implements Comparable {
int english=0;
String name;
CollegeStu(int english,String name) {
this.name=name;
this.english=english;
}
@Override
public int compareTo(Object b) {
CollegeStu stu=(CollegeStu)b;
return (this.english-stu.english);
}
}
public class Exam2 {
public static void main(String[] args) {
List list=new LinkedList();
int score []={99, 88, 77, 66, 55};
String name []={"谭笑","李天林","孔月","陈思兵" ,"刘宇坤"};
for (int i=0;i iter=list.iterator();
TreeSet mytree=new TreeSet();
while (iter.hasNext()) {
CollegeStu stu=iter.next();
mytree.add(stu);
System.out.println(" "+stu.name+" "+stu.english);
}
}
}
- 有10个U盘,有两个重要的属性:价格和容量,编写一个应用程序,使用TreeMap
类,分别按照价格和容量排序输出10个U盘的详细信息
import java.util.*;
class UDiscKey implements Comparable {
double key=0;
UDiscKey(double d) {
key=d;
}
public int compareTo(Object b) {
UDiscKey disc=(UDiscKey)b;
if((this.key-disc.key)==0)
return -1;
else
return (int)((this.key-disc.key)*1000);
}
}
class UDisc{
int amount;
double price;
UDisc(int m,double e) {
amount=m;
price=e;
}
}
public class Ex3 {
public static void main(String args[ ]) {
TreeMap treemap= new TreeMap();
int amount[]={1,2,4,8,16,32,64,128,256,512,1024};
double price[]={40,20,10,80,300,220,380,400,600,500};
UDisc UDisc[]=new UDisc[10];
for(int k=0;k collection=treemap.values();
Iterator iter=collection.iterator();
while(iter.hasNext()) {
UDisc disc=iter.next();
System.out.println(""+disc.amount+"G "+disc.price+"元"); }
treemap.clear();
for(int k=0;k