课下作业(选做)第十周

一 相关知识点总结

创建一个空的链表

List<Student> list = new LinkedList<Student>();

向链表中添加新的结点

list.add(new Student());

删除结点

list.remove("");

链表中数据的插入

list.add("");

链表中数据的排序

Collections.sort();

将list中的元素按升序排序

public static sort(List<E>list)

二 课上内容补做

排序
import java.util.*;
public class Example {public  static void main(String[] args) {List<Student> list = new LinkedList<>();list.add(new Student(20165217,"叶  佺",99,91,89));list.add(new Student(20165218,"赵冰雨",76,66,95));list.add(new Student(20165219,"王彦博",77,81,68));list.add(new Student(20165220,"葛宇豪",89,45,66));list.add(new Student(20165221,"谭  笑",82,80,86));SortTotal_score sortBytotal_score = new SortTotal_score();Collections.sort(list, sortBytotal_score);SortID sortByID = new SortID();Collections.sort(list, sortByID);System.out.println("学号排序:");for (Student student : list) {System.out.println(student);}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; //表示平均成绩@Overridepublic String toString() {return " "+name+" "+id+" "+total_score;}public Student(int id, String name,double computer_score,double english_score,double maths_score) {this.id = id;this.name = name;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 SortID implements Comparator<Student> {@Overridepublic int compare(Student a, Student b) {return a.getId() - b.getId();}}
class SortTotal_score implements Comparator<Student> {@Overridepublic int compare(Student a, Student b) {return (int)( a.getTotalScore() - b.getTotalScore());}
}

单链表
import java.util.*;
public class Mylist {public static void main(String [] args) {//选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点Node<String> node1 =new Node<String>("20165217",null);Node<String> node2 =new Node<String>("20165218",null);Node<String> node3 =new Node<String>("20165220",null);Node<String> node4 =new Node<String>("20165221",null);//把上面四个节点连成一个没有头结点的单链表node1.next=node2;node2.next=node3;node3.next=node4;node4.next=node1;//遍历单链表,打印每个结点的Node<String> node =node1 ;System.out.println("打印插入前的链表数据");for (int i=0;i<4;i++) {System.out.println(node.data.toString());node = node.next;}//把你自己插入到合适的位置(学号升序)//遍历单链表,打印每个结点的System.out.println("打印插入后的链表数据");node=node1;Node<String> s=new Node<String>("20165219",null);for(int i=0;i<5;i++){System.out.println(node.data.toString());node = node.next;if(node==node2){node2.next=s;s.next=node3;}}//从链表中删除自己node=node1;for(int i=0;i<4;i++){node = node.next;if(node==s){s=null;node2.next=node3;node3.next=node4;}}node =node1 ;System.out.println("打印删除后的链表数据");for (int i=0;i<4;i++) {System.out.println(node.data.toString());node = node.next;}}
}class Node<String>                             //单链表结点类,T指定结点的元素类型
{public String data;                               //数据域,存储数据元素public Node<String> next;                         //地址域,引用后继结点public Node(String data, Node<String> next)            //构造结点,data指定数据元素,next指定后继结点{this.data = data;                        //T对象引用赋值this.next = next;                        //Node<T>对象引用赋值}public Node(){this(null, null);}@Overridepublic java.lang.String toString()                     //返回结点数据域的描述字符串{return this.data.toString();}
}

教材代码分析

class Cone<E> { double height;E bottom;           //用泛型类E声明对象bottompublic Cone (E b) {bottom=b;   //给泛型类对象赋值}public void setHeight(double h) {height=h;}public double computerVolume() {String s=bottom.toString();//泛型变量只能调用从Object类继承的或重写的方法double area=Double.parseDouble(s); return 1.0/3.0*area*height; }
}class Rect {double sideA,sideB,area; Rect(double a,double b) {sideA=a;sideB=b;} public String toString() {area=sideA*sideB;return ""+area;}
}class Circle {double area,radius; Circle(double r) {radius=r;} public String toString() { //重写Object类的toString()方法area=radius*radius*Math.PI;return ""+area;}
}public class Example15_1 {public static void main(String args[]) {Circle circle=new Circle(10);Cone<Circle> coneOne=new Cone<Circle>(circle);//创建一个(圆)锥对象  coneOne.setHeight(16);System.out.println(coneOne.computerVolume());Rect rect=new Rect(15,23);Cone<Rect> coneTwo=new Cone<Rect>(rect);//创建一个(方)锥对象coneTwo.setHeight(98); System.out.println(coneTwo.computerVolume());}
}

import java.util.*;
public class Example15_2 {public static void main(String args[]){List<String> list=new LinkedList<String>();//定义一个空链表for(int i=0;i<=60096;i++){list.add("speed"+i);}Iterator<String> iter=list.iterator();  //获取链表中的迭代器long starttime=System.currentTimeMillis();while(iter.hasNext()){String te=iter.next();}//进行遍历long endTime=System.currentTimeMillis();long result=endTime-starttime;System.out.println("使用迭代器遍历集合所用时间:"+result+"毫秒");starttime=System.currentTimeMillis();for(int i=0;i<list.size();i++){String te=list.get(i);}endTime=System.currentTimeMillis();result=endTime-starttime;System.out.println("使用get方法遍历集合所用时间:"+result+"毫秒");}
}

import java.util.*;
public class Example15_3 {public static void main(String args[]){LinkedList mylist=new LinkedList();//定义一个空链表mylist.add("你");                 //链表中的第一个节点mylist.add("好");                 //链表中的第二个节点int number=mylist.size();         //获取链表的长度for(int i=0;i<number;i++){String temp=(String)mylist.get(i); //必须强制转换取出的数据System.out.println("第"+i+"节点中的数据:"+temp);} Iterator iter=mylist.iterator();while(iter.hasNext()) {String te=(String)iter.next();  //必须强制转换取出的数据System.out.println(te);}}
}

import java.util.*;
class Student implements Comparable { int height=0;String name;Student(String n,int h) {name=n;height = h;}public int compareTo(Object b) { // 两个Student对象相等当且仅当二者的height值相等Student st=(Student)b;return (this.height-st.height);}
}
public class Example15_4 {public static void main(String args[ ]) { List<Student> list = new LinkedList<Student>();//定义一个空链表list.add(new Student("张三",188));list.add(new Student("李四",178));list.add(new Student("周五",198)); //向链表中依次添加元素Iterator<Student> iter=list.iterator();System.out.println("排序前,链表中的数据");while(iter.hasNext()){Student stu=iter.next();System.out.println(stu.name+ "身高:"+stu.height);}//对链表进行遍历Collections.sort(list);//进行升序排序System.out.println("排序后,链表中的数据");iter=list.iterator();while(iter.hasNext()){Student stu=iter.next();System.out.println(stu.name+ "身高:"+stu.height);}Student zhaoLin = new Student("zhao xiao lin",178);int index = Collections.binarySearch(list,zhaoLin,null);if(index>=0) {System.out.println(zhaoLin.name+"和链表中"+list.get(index).name+"身高相同");}}
}

import java.util.*;
public class Example15_5 {public static void main(String args[ ]) { List<Integer> list = new LinkedList<Integer>();//定义一个空链表for(int i=10;i<=50;i=i+10)list.add(new Integer(i));System.out.println("洗牌前,链表中的数据");Iterator<Integer> iter=list.iterator();while(iter.hasNext()){Integer n=iter.next();System.out.printf("%d\t",n.intValue());}//遍历链表Collections.shuffle(list);//按洗牌算法重新随机排列System.out.printf("\n洗牌后,链表中的数据\n");iter=list.iterator();while(iter.hasNext()){Integer n=iter.next();System.out.printf("%d\t",n.intValue());}System.out.printf("\n再向右旋转1次后,链表中的数据\n");Collections.rotate(list,1);//旋转链表中的数据iter=list.iterator();while(iter.hasNext()){Integer n=iter.next();System.out.printf("%d\t",n.intValue());}}
}

import java.util.*;
public class Example15_6 {public static void main(String args[]) {Stack<Integer> stack=new Stack<Integer>();//定义一个堆栈对象stack.push(new Integer(1)); stack.push(new Integer(1));//进行压栈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(f1+f2);System.out.println(""+temp.toString()); stack.push(temp);stack.push(F2);k++;}} }
}

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 Example15_8 {public static void main(String args[]) {TreeSet<Student> mytree=new TreeSet<Student>();//定义一个树集对象Student st1,st2,st3,st4;st1=new Student(90,"赵一");st2=new Student(66,"钱二");st3=new Student(86,"孙三");st4=new Student(76,"李四");mytree.add(st1);mytree.add(st2);mytree.add(st3);mytree.add(st4);//依次添加数据Iterator<Student> te=mytree.iterator();while(te.hasNext()) {Student stu=te.next();System.out.println(""+stu.name+" "+stu.english);//遍历并打印数据}}
}

课后编程题

第一题

import java.util.*;
public class Series {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(temp);stack.push(F2);k++;}}}
}

第二题

import java.util.*;
public class bce {public static void main(String args[]) {TreeSet<Score> mytree = new TreeSet<>();List<Score> mylist = new LinkedList<Score>();mylist.add(new Score("赵一", 89));mylist.add(new Score("钱二", 99));mylist.add(new Score("孙三", 100));mylist.add(new Score("李四", 92));mylist.add(new Score("周五", 88));//添加节点Iterator<Score> score = mylist.iterator();System.out.println("排序之前的:");while (score.hasNext()) {Score sco = score.next();mytree.add(sco);System.out.println(sco.name + " " + sco.english);}//将链表中的成绩放入树集中System.out.println("排序之后的:");Iterator<Score> te = mytree.iterator();while (te.hasNext()) {Score stu = te.next();System.out.println("" + stu.name + " " + stu.english);}}
}class Score implements Comparable {int english = 0;String name = "";Score(String name, int english) {this.name = name;this.english = english;}@Overridepublic int compareTo(Object a) {Score b = (Score) a;return this.english - b.english;}}

第三题

import java.util.*;
class Sort implements Comparable {double d=0;Sort (double d) {this.d=d;}@Overridepublic int compareTo(Object b) {Sort st=(Sort) b;if((this.d-st.d)==0) {return -1;}else {return (int) ((this.d - st.d) * 1000);}}
}
class U {String name="";double speed,capacity;U(String name,double speed,double capacity) {this.name=name;this.capacity=capacity;this.speed=speed;}
}
public class PriceSort {public static void main(String args[ ]) {TreeMap<Sort,U>  treemap= new TreeMap<Sort,U>();String name[]={"A","B","C","D","E","F","G","H","I","J"};double speed[]={111,100,101,345,213,433,565,333,454,454};double capacity[]={4,10,32,64,128,34,68,120,112,1024};U u[]=new U[10];for(int k=0;k<u.length;k++) {u[k]=new U(name[k],speed[k],capacity[k]);}Sort key[]=new Sort[10] ;for(int k=0;k<key.length;k++) {key[k]=new Sort(u[k].speed);}for(int k=0;k<u.length;k++) {treemap.put(key[k],u[k]);}System.out.println("按speed排序:");Collection<U> collection=treemap.values();Iterator<U> iter=collection.iterator();while(iter.hasNext()) {U stu=iter.next();System.out.println("品牌"+stu.name+"speed:"+stu.speed);}treemap.clear();for(int k=0;k<key.length;k++) {key[k]=new Sort(u[k].capacity);}for(int k=0;k<u.length;k++) {treemap.put(key[k],u[k]);}System.out.println("按capacity排序:");collection=treemap.values();iter=collection.iterator();while(iter.hasNext()) {U stu=(U)iter.next();System.out.println("品牌"+stu.name+" capacity :"+stu.capacity);}}
}

转载于:https://www.cnblogs.com/wyb-1998/p/8999380.html

课下作业(选做)第十周相关推荐

  1. 20165204 第十周课下作业补做

    20165204 第十周课下测试补做 课上测试内容补做 测试2 针对下面的Student类,使用Comparator编程完成以下功能: 在测试类StudentTest中新建学生列表,包括自己和学号前后 ...

  2. 课下作业(选做)第八周

    课下作业(选做)第八周 课上内容补做: 由于我的电脑之前始终不能连接上数据库,无法通过http://localhost来进入,总是显示服务器被拒绝,导致当时我没能做出.后来,查阅了许多资料并在王老师的 ...

  3. 2017-2018-1 20155320第十周课下作业-IPC

    2017-2018-1 20155320第十周课下作业-IPC 研究Linux下IPC机制:原理,优缺点,每种机制至少给一个示例,提交研究博客的链接 共享内存 管道 FIFO 信号 消息队列 共享内存 ...

  4. 20165332第八周课下作业

    20165332第八周课下作业 相关知识点总结 MYSQL数据库的安装与使用 JDBC语句连接数据库,关闭连接 循序查询.条件与排序查询 添加与删除操作 通用查询和事务管理 课下补做 代码分析 Exa ...

  5. 20175221 MyCP(课下作业,必做)

    MyCP(课下作业,必做) 任务详情 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: - java MyCP -tx XXX1.txt XXX2 ...

  6. MyCP(课下作业,必做)

    20175334 MyCP(课下作业,必做) 题目要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.t ...

  7. 20155212 2017-2018-1 《信息安全系统设计》第8周课下作业

    20155212 2017-2018-1 <信息安全系统设计>第8周课下作业 题目1 完成家庭作业4.47,4.48,4.49 相应代码反汇编成X86-64汇编 把上述X86-64汇编翻译 ...

  8. 数据库MySQL(课下作业,必做)

    数据库MySQL(课下作业,必做) 题目要求: 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入 ...

  9. 2017-2018-1 20155321 《信息安全系统设计基础》课下作业3

    2017-2018-1 20155321 <信息安全系统设计基础>课下作业3 课堂练习第五题 因为虚拟机是64位的,所以先输入命令sudo apt-get install libc6-de ...

  10. 20165208 课下作业

    20165208 课下作业 相关知识点总结 JDB vim 编译 进入第二个标签 使用javac -g -d bin src/HelloJDB.java对程序进行编译. Alt+3 进入第三个标签 使 ...

最新文章

  1. apache 安装与配置详细教程
  2. 2009年出现的计算机术语,2009年计算机一级考试真题及答案
  3. 数据挖掘十大经典算法(9) 朴素贝叶斯分类器 Naive Bayes
  4. DataTable 中各种计算(笔记)
  5. 转 jquery使用技巧小结
  6. 董明珠和雷军的十亿赌约马上就要到了,谁会笑到最后?
  7. 什么是消防产品3C认证?
  8. PCB板上的蓝宝石---关于光学定位点的DFM
  9. amr-nb linux 编译安装包,3GPP最新版本amr-nb编解码源代码
  10. MATLAB无线计算器
  11. 如何在Office 365中使用Office剪贴板?
  12. Oracle官方JDBC jar包下载
  13. Team Foundation Power Tools 1.2发布
  14. 互联网公司招聘,你需要注意这四点
  15. 重读《学习JavaScript数据结构与算法-第三版》- 第6章 链表(一)
  16. 【DL with Pytorch】第 3 章 :使用 DNN 的分类问题
  17. consistent equation
  18. 渗透测试第一弹:信息刺探
  19. 火狐主页被篡改为2345的解决办法
  20. 基于SDN的DDoS攻击检测和防御方法

热门文章

  1. 【总结】1334- JS中Object的keys是无序的吗
  2. 金融数据获取系列之一(优矿)
  3. dubbo admin安装中易踩坑点及解决方法
  4. 滴滴分析专家8000字干货:数据如何驱动业务增长 ?
  5. Chapter 5 Eigenvalues and Eigenvectors
  6. 摩拜app显示未能连接到服务器,摩拜单车又现大面积故障?回应称未接到反馈
  7. Git 奇淫技巧:Github ID 更名奇遇记
  8. 苹果app-H5封装源码-一键封装app搭建
  9. windows简单命令
  10. mybatis中使用DATE_SUB()函数实现网站访问量日,月,年统计