第十周课下作业
1. 相关知识点的总结
(1)单链表
- 创建单链表
- 链表中数据的插入list.add("**");
- 链表中数据的排序Collections.sort();
链表中数据的删除lsit.remove("");
(2)排序- 树集概念
- 树映射 TreeMap
适合用于数据的排序 - 通过关键字进行排序TreeMap
treemap= new TreeMap (); 对数据进行排序(比较comparable和comparator)
在List或数组中的对象如果没有实现Comparable接口时,那么就需要调用者为需要排序的数组或List设置一个Compartor,Compartor的compare方法用来告诉代码应该怎么去比较两个实例,然后根据比较结果进行排序
comparator
package java.util;
public interface Comparator {
int compare(T o1, T o2);
boolean equals(Object obj);
}
comparable
package java.lang;
import java.util.*;
public interface Comparable {
public int compareTo(T o);
}
2.课上内容的补做,结果截图
(1)代码检查
代码是正确的课上出现的问题:
下来查询了资料:解决 java “错误:编码GBK 的不可映射字符” 将问题解决
(2)
排序
代码:
import java.util.List;
import java.lang.String;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class StudentTest {
class Stu{
public int age;
public String name;
public int id;
public int english_score;
public int computer_score;
public int maths_score;
public int total_score;
public Stu(int id, String name,int english_score,int computer_score,int maths_score,int total_score) {
super();
this.id = id;
this.name = name;
this.english_score = english_score;
this.computer_score = computer_score;
this.maths_score = maths_score;
this.total_score = total_score;
}
@Override
public String toString() {
return ( "\n"+" 学号 " + id + " 姓名 " + name +" 英语 "+english_score+" 计算机 "+computer_score+" 数学 "+maths_score+" 总成绩 "+total_score+"\n");
}
}
public static void main(String[] args) {
List list= new ArrayList<>();
list.add(new StudentTest().new Stu(20165325,89,67,78,234));
list.add(new StudentTest().new Stu(20165326, 78,90,98,266));
list.add(new StudentTest().new Stu(20165327, 45,66,87,198));
list.add(new StudentTest().new Stu(20165328, 88,90,88,266));
list.add(new StudentTest().new Stu(20165329, 76,56,89,221));
Collections.sort(list, new Comparator() {
@Override
public int compare(Stu o1, Stu o2) {
return o1.id - o2.id;
}
});
System.out.println("按照学号排序:"+list);
Collections.sort(list, new Comparator() {
@Override
public int compare(Stu o1, Stu o2) {
return o1.total_score - o2.total_score;
}
});
System.out.println("按总成绩顺序排序:"+list);
}
}
单链表
代码:
import java.util.*;
public class MyList {
public static void main(String [] args) {
List mylist=new LinkedList();
//选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
mylist.add("20165325");
mylist.add("20165326");
mylist.add("20165328");
mylist.add("20165329");
//把上面四个节点连成一个没有头结点的单链表
System.out.println("打印初始链表");
//遍历单链表,打印每个结点的
Iterator iter=mylist.iterator();
while (iter.hasNext()){
String num=iter.next();
System.out.println(num);
}
//把你自己插入到合适的位置(学号升序)
mylist.add("20165327");
Collections.sort(mylist);
//遍历单链表,打印每个结点的
System.out.println("插入我的学号在排序之后,链表中的数据:");
iter =mylist.iterator();
while(iter.hasNext()){
String num=iter.next();
System.out.println(num);
}
//从链表中删除自己
mylist.remove("20165327");
System.out.println("删除我的学号之后打印链表:");
//遍历单链表,打印每个结点的
iter=mylist.iterator();
while(iter.hasNext()){
String num=iter.next();
System.out.println(num);
}
}
}
3. 教材第十五章的代码分析
4.补做教材第十五章的编程题目
(1)使用堆栈结构输出an的若干项,其中an=2an-1 +2an-2 ,a1=3,a2=8
import java.util.*;
public class E {
public static void main(String args[]) {
Stack stack=new Stack
(2)编写一个程序,将链表中的学生英语成绩单存放到一个树集中,使得按成绩自动排序,并输出排序结果
import java.util.*;
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);
}
}
public class E {
public static void main(String args[]) {
List list=new LinkedList();
int score []={65,76,45,99,77,88,100,79};
String name[]={"张三","李四","旺季","加戈","为哈","周和","赵李","将集"};
for(int i=0;i iter=list.iterator();
TreeSet mytree=new TreeSet();
while(iter.hasNext()){
Student stu=iter.next();
mytree.add(stu);
}
Iterator te=mytree.iterator();
while(te.hasNext()) {
Student stu=te.next();
System.out.println(""+stu.name+" "+stu.english);
}
}
}
(3)有10个U盘,有两个重要的属性:价格和容量。编写一个应用程序,使用TreeMap
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 E {
public static void main(String args[ ]) {
TreeMap treemap= new TreeMap();
int amount[]={1,2,4,8,16};
double price[]={867,266,390,556};
UDisc UDisc[]=new UDisc[4];
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