20165326第十周课上测试补做
知识点总结
实验主要涉及了链表和泛型的知识点
泛型
- 使用
class 类名<>
声明一个类,要用具体的类型替换泛型
链表
- 有若干个节点对象组成的数据结构
- 使用
iterator()
方法获取对象 - 树集
TreeSet<>
,使用.add()
为树集添加节点
课上代码
1
针对下面的Student类,使用Comparator编程完成以下功能:
1、在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后)
2、对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码
Student类老师已经给出,直接将下面的两个Comparator代码加入即可
代码
import java.util.*;
public class IDComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Student st1 = (Student)o1;
Student st2 = (Student)o2;
return (Integer.parseInt(st1.getId())-Integer.parseInt(st2.getId()));
}
}
---
import java.util.*;
public class scoreComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Student st1 = (Student)o1;
Student st2 = (Student)o2;
return (int) (st1.getTotalScore()-st2.getTotalScore());
}
}
运行截图
2
代码
import java.util.*;
public class MyList {
public static void main(String [] args) {
List<String> list=new LinkedList<String>();
list.add("20165325,dj");
list.add("20165324,cj");
list.add("20165327,jt");
list.add("20165328,jw");
System.out.println("打印初始链表");
//把上面四个节点连成一个没有头结点的单链表
Iterator<String> iter=list.iterator();
while(iter.hasNext()){
String te=iter.next();
System.out.println(te);
}
//遍历单链表,打印每个结点的
list.add("20165326,cz");
//把你自己插入到合适的位置(学号升序)
System.out.println("插入我的学号后排序,打印链表");
Collections.sort(list);
iter=list.iterator();
while(iter.hasNext()){
String te=iter.next();
System.out.println(te);
}
//遍历单链表,打印每个结点的
list.remove("20165326,cz");
//从链表中删除自己
System.out.println("删除我的学号后打印链表");
iter=list.iterator();
while(iter.hasNext()){
String te=iter.next();
System.out.println(te);
}
//遍历单链表,打印每个结点的
}
}
运行截图
ch15课后习题
(1)使用堆栈结构输出an的若干项,其中a_n=2a_n-1+2a_n=2a_(n-1)+2a_(n-2),a_1=3,a_2=8
import java.util.*;
public class E1 {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.push(new Integer(3));
stack.push(new Integer(8));
int k = 1;
while(k<=10){
for(int i=1;i<=2;i++){
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(F2);
k++;
}
}
}
}
(2)编写一个程序,将链表中的学生英语成绩单存放到一个树集中,使得按成绩自动排序,并输出排序结果.
import java.util.*;
class Student2 implements Comparable{
int english = 0;
String name;
Student2(int english,String name){
this.name = name;
this.english = english;
}
@Override
public int compareTo(Object b){
Student2 st = (Student2)b;
return (this.english-st.english);
}
}
public class E2 {
public static void main(String[] args) {
List<Student2> list = new LinkedList<Student2>();
int score[] = {98,100,99,96,95,94};
String name [] = {"Re","Or","Ye","Gr","Bl","Pu"};
for(int i=0;i<score.length;i++){
list.add(new Student2(score[i],name[i]));
}
Iterator<Student2> iter = list.iterator();
TreeSet<Student2> mytree = new TreeSet<Student2>();
while(iter.hasNext()){
Student2 stu = iter.next();
mytree.add(stu);
}
Iterator<Student2> te = mytree.iterator();
while (te.hasNext()){
Student2 stu = te.next();
System.out.println(""+stu.name+" "+stu.english);
}
}
}
运行结果
(3)有10个U盘,有两个重要属性:价格和容量。编写一个应用程序,使用TreeMap<K,V>类,分别按照价格和容量排序输出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 E3 {
public static void main(String[] args) {
TreeMap<UDiscKey,UDisc> treeMap = new TreeMap<UDiscKey,UDisc>();
int amount[] = {1,2,3,4,8,16,32,64,128,10};
double price[] = {10,20,30,40,70,60,70,100,90,80};
UDisc UDisc[] = new UDisc[10];
for(int i=0;i<UDisc.length;i++){
UDisc[i]= new UDisc(amount[i],price[i]);
}
System.out.println("按容量排序如下:");
UDiscKey key[]= new UDiscKey[10];
for(int i=0;i<key.length;i++){
key[i] = new UDiscKey(UDisc[i].amount);
}
for(int i=0;i<UDisc.length;i++){
treeMap.put(key[i],UDisc[i]);
}
int number = treeMap.size();
Collection<UDisc> collection = treeMap.values();
Iterator<UDisc> iter = collection.iterator();
while(iter.hasNext()){
UDisc disc = iter.next();
System.out.println(""+disc.amount+"G "+disc.price+"元");
}
treeMap.clear();
System.out.println("按价格排序如下:");
for(int i=0;i<key.length;i++){
key[i] = new UDiscKey(UDisc[i].price);
}
for(int i=0;i<UDisc.length;i++){
treeMap.put(key[i],UDisc[i]);
}
number = treeMap.size();
collection = treeMap.values();
iter = collection.iterator();
while (iter.hasNext()){
UDisc disc = iter.next();
System.out.println(""+disc.amount+"G "+disc.price+"元");
}
}
}
运行结果