知识总结

java.util包中的LinkedList

遍历:遍历链表,利用迭代器。链表对象可以使用iterator()方法获取一个Iterator对象,该对象就是针对当前链表的迭代器。

创建一个空的链表

List list = new LinkedList();

向链表中添加新的结点

list.add(new Student(

删除结点

list.remove("

查找:

int binarySearch(List

将list中的元素按升序排序

public static sort(List

链表中数据的插入

list.add("

课上内容补做

数据结构-排序

在数据结构和算法中,排序是很重要的操作,要让一个类可以进行排序,有两种方法:

  • 有类的源代码,针对某一成员变量排序,让类实现Comparable接口,调用Collection.sort(List)
  • 没有类的源代码,或者多种排序,新建一个类,实现Comparator接口 调用Collection.sort(List, Compatator)

针对下面的Student类,使用Comparator编程完成以下功能:

  1. 在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后
  2. 对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码
  3. 课下提交代码到码云

    代码链接:

    https://gitee.com/BESTI-IS-JAVA-2018/20165305zhenlong/tree/master/java/Student

    结果截图

数据结构-单链表

参见附件,补充MyList.java的内容,提交运行结果截图(全屏)
课下推送代码到码云

代码链接

https://gitee.com/BESTI-IS-JAVA-2018/20165305zhenlong/tree/master/java/MyList

结果截图

第十五章的代码分析

例子1

Example15_1.java ,Cone.java , Rect.java , Circle.java 中,声明了一个泛型类:Cone,一个Cone对象计算体积时,只关心它的底是否能计算面积,并不关心底的类型
Cone.java

public class Cone<E> {            double height;                              E bottom;    //用泛型类E声明对象bottom              public Cone(E b) {  //参数b是泛型类型                  bottom = b;               }                                                             public void setHeight(double h) { //此方法给高height赋值         height = h;           }                                                        public double computerVolume() {  //计算体积的方法                                    String s = bottom.toString();//泛型变量只能调用从Object类继承的或重写的方法        double area = Double.parseDouble(s); //将string类型转换成double型              return 1.0 / 3.0 * area * height;  //计算体积                  }
}                  

Rect.java

public class Rect {  //计算方形面积的类             double sideA, sideB, area;                                         Rect(double a, double b) { //构造方法将a,b值传给sideA,sideB         sideA = a;      sideB = b;            }                                                                                                       public String toString() {//泛型类中的泛型变量bottom只能调用Object类中的方法,Rect类重写了toString()方法area = sideA * sideB; //计算面积return "" + area;}
}

Circle

public class Circle {//计算圆形面积的类double area, radius;Circle(double r) {//构造方法将r值传给radiusradius = r;}public String toString() { //泛型类中的泛型变量bottom只能调用Object类中的方法,Circle类重写Object类的toString()方法area = radius * radius * Math.PI;//计算面积,Math.PI为圆周率return "" + area;}
}

Example15_1.java

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);//调用Cone类中setHeight()方法,将高设为16System.out.println(coneOne.computerVolume());//调用Cone类中computerVolume()方法,计算体积;computerVolume()方法调用Circle类中的toString()方法,计算面积Rect rect = new Rect(15, 23);Cone<Rect> coneTwo = new Cone<Rect>(rect);//创建一个(方)锥对象coneTwo.setHeight(98);System.out.println(coneTwo.computerVolume());//下面方锥体积和上面计算圆锥体积的道理相同}
}

例子2

比较了使用迭代器遍历链表和get(int index)方法遍历链表的所用时间

import java.util.*;
public class Example15_2 {public static void main(String args[]) {List<String> list = new LinkedList<String>();//创建链表listfor (int i = 0; i <= 60096; i++) {list.add("speed" + i);//添加结点}Iterator<String> iter = list.iterator();//创建迭代器iterlong starttime = System.currentTimeMillis();//获取当前时间while (iter.hasNext()) {//hasNext()此方法检查链表中是否还有结点String te = iter.next();//next()方法获取下一个结点}long endTime = System.currentTimeMillis();//获取遍历完链表的时间long result = endTime - starttime;//时间差代表遍历链表的时间System.out.println("使用迭代器遍历集合所用时间:" + result + "毫秒");starttime = System.currentTimeMillis();for (int i = 0; i < list.size(); i++) {//list.size()为链表结点数String te = list.get(i);//list.get()依次得到链表中的结点数据}endTime = System.currentTimeMillis();result = endTime - starttime;System.out.println("使用get方法遍历集合所用时间:" + result + "毫秒");}
}  

例子3

代码中使用了JDK1.5版本之前的LinkedList。

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);//hasNext()方法和next()方法在前面解释过了}}
}

例子4

Student类通过实现Comparable接口规定该类的对象的大小关系(按height值的大小确定大小关系,即学生按其身高确定之间的大小关系)。链表添加了3个Student对象,Collections调用sort方法将链表中的对象按身其height值升序排序,并查找一个对象的height值是否和链表中某个对象的height值相同。

import java.util.*;
class Student implements Comparable {//Student类通过实现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>();//创建链表listlist.add(new Student("张三", 188));list.add(new Student("李四", 178));list.add(new Student("周五", 198));//向链表中添加3个Student对象Iterator<Student> iter = list.iterator();//创建迭代器System.out.println("排序前,链表中的数据");while (iter.hasNext()) {Student stu = iter.next();//创建Student对象stu存放结点的数据System.out.println(stu.name + "身高:" + stu.height);//调用成员变量}Collections.sort(list);//Collections类提供用于排序和查找的方法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) {//没有找到index=-1,找到index>=0System.out.println(zhaoLin.name + "和链表中" + list.get(index).name + "身高相同");}
}
}
例子5

代码中使用了shuffle()方法、reverse()方法和rotate()。

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());//输出下一下int类型的值}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());}}
}

例子6

代码中用堆栈输出该递归序列的若干项 。

import java.util.*;
public class Example15_6 {public static void main(String args[]) {Stack<Integer> stack = new Stack<Integer>();//建立一个堆栈对象stack.push(new Integer(1));//压栈,第一项为1stack.push(new Integer(1));//压栈,第二项为1int k = 1;while (k <= 10) {//实现Fibonacci整数序列的前12项for (int i = 1; i <= 2; i++) {Integer F1 = stack.pop();//取出栈顶对象int f1 = F1.intValue();//得到对象的int值Integer F2 = stack.pop();//取出栈顶对象int f2 = F2.intValue();//得到对象的int值Integer temp = new Integer(f1 + f2);//创建f1和f2之和的对象System.out.println("" + temp.toString());stack.push(temp);//压栈stack.push(F2);//将刚才后取出的对象压栈k++;//实现递归循环}}}
}

例子7

这是一个英语单词查询的GUI程序,用户在界面的的一个文本框中输入一个英文单词回车确认,另一个文本框显示英文单词的汉语翻译。

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class WordPolice implements ActionListener {JTextField showText;HashMap<String, String> hashtable;//创建散列映射对象File file = new File("word.txt"); //创建文件对象Scanner sc = null;WordPolice() {hashtable = new HashMap<String, String>();try {sc = new Scanner(file);//使用Scanner解析word.txt中的单词while (sc.hasNext()) {String englishWord = sc.next();String chineseWord = sc.next();//根据word.txt文件可知先是英文单词才是汉语hashtable.put(englishWord, chineseWord);//将英文单词、汉语作为键/值对储存在散列映射中}} catch (Exception e) {}}public void setJTextField(JTextField showText) {this.showText = showText;}public void actionPerformed(ActionEvent e) {String englishWord = e.getActionCommand();if (hashtable.containsKey(englishWord))//若有使用englishWord这个键的键/值对 {String chineseWord = hashtable.get(englishWord);//返回使用englishWorld键所对应的值showText.setText(chineseWord);//将值输出} else {showText.setText("没有此单词");}}
}

例子8

代码中的树集按着英语成绩从底到高存放四个Student对象。

import java.util.*;
class Student implements Comparable {
//Student类通过实现Comparable接口规定按english确定大小关系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(new Student(90,"赵一"));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);//依次输出排序好的结点}}
}

例子9

使用了TreeMap,分别按着学生的英语成绩和数学成绩排序节点。

import java.util.*;
class StudentKey implements Comparable {//StudentKey类通过实现Comparable接口规定按关键字进行排序double d = 0;StudentKey(double d) {this.d = d;}public int compareTo(Object b) {StudentKey st = (StudentKey) b;if ((this.d - st.d) == 0)return -1;elsereturn (int) ((this.d - st.d) * 1000);}
}
class Student {String name = null;double math, english;Student(String s, double m, double e) {name = s;math = m;english = e;}
}
public class Example15_9 {public static void main(String args[]) {TreeMap<StudentKey, Student> treemap = new TreeMap<StudentKey, Student>();//创建一个树映射,StudentKey为关键字,Student为数值String str[] = {"赵一", "钱二", "孙三", "李四"};double math[] = {89, 45, 78, 76};double english[] = {67, 66, 90, 56};Student student[] = new Student[4];for (int k = 0; k < student.length; k++) {student[k] = new Student(str[k], math[k], english[k]);}StudentKey key[] = new StudentKey[4];for (int k = 0; k < key.length; k++) {key[k] = new StudentKey(student[k].math); //关键字按数学成绩排列大小}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.math);}treemap.clear();//清空树映射,为下一轮排序进行准备for (int k = 0; k < key.length; k++) {key[k] = new StudentKey(student[k].english);//关键字按英语成绩排列大小}for (int k = 0; k < student.length; k++) {treemap.put(key[k], student[k]);}number = treemap.size();System.out.println("树映射中有" + number + "个对象:按英语成绩排序:");collection = treemap.values();iter = collection.iterator();while (iter.hasNext()) {Student stu = (Student) iter.next();System.out.println("姓名 " + stu.name + " 英语 " + stu.english);}//按照英语成绩排序和按照数学成绩排序类似,只是更换了关键字}
}

例子10

代码中使用了自动装箱与拆箱。

import java.util.*;
public class Example15_10 {public static void main(String args[]) {ArrayList<Integer> list = new ArrayList<Integer>();for (int i = 0; i < 10; i++) {list.add(i);  //自动装箱,实际添加到list中的是new Integer(i)。}for (int k = list.size() - 1; k >= 0; k--) {int m = list.get(k);  //自动拆箱,获取Integer对象中的int型数据System.out.printf("%3d", m);}}
}

例子11

代码中使用对象流实现商品库存的录入与显系示统。
Example15_11.java

public class Example15_11 {public static void main(String args[]) {WindowGoods win=new WindowGoods();win.setTitle("商品的录入与显示");}
}

Goods.java

public class Goods implements java.io.Serializable {String name, mount,price;public void setName(String name) {this.name=name;}public void setMount(String mount) {this.mount=mount;}public void setPrice(String price) {this.price=price;}  public String getName() {return name;}public String getMount() {return mount;}public String getPrice() {return price;}
}

InputArea.java

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class InputArea extends JPanel implements ActionListener {File f=null;                   //存放链表的文件Box baseBox ,boxV1,boxV2; JTextField name,mount,price;  //为Goods对象提供的视图JButton button;               //控制器LinkedList<Goods> goodsList; //存放Goods对象的链表InputArea(File f) {this.f=f;goodsList=new LinkedList<Goods>();name=new JTextField(12);mount=new JTextField(12); price=new JTextField(12);button=new JButton("录入");button.addActionListener(this);boxV1=Box.createVerticalBox();boxV1.add(new JLabel("输入名称"));boxV1.add(Box.createVerticalStrut(8));boxV1.add(new JLabel("输入库存"));boxV1.add(Box.createVerticalStrut(8));boxV1.add(new JLabel("输入单价"));boxV1.add(Box.createVerticalStrut(8));boxV1.add(new JLabel("单击录入"));boxV2=Box.createVerticalBox();boxV2.add(name);boxV2.add(Box.createVerticalStrut(8));boxV2.add(mount);boxV2.add(Box.createVerticalStrut(8));boxV2.add(price);boxV2.add(Box.createVerticalStrut(8));boxV2.add(button);baseBox=Box.createHorizontalBox();baseBox.add(boxV1);baseBox.add(Box.createHorizontalStrut(10));baseBox.add(boxV2);add(baseBox); }public void actionPerformed(ActionEvent e) {if(f.exists()) {try{FileInputStream  fi=new FileInputStream(f);ObjectInputStream oi=new ObjectInputStream(fi);goodsList= (LinkedList<Goods>)oi.readObject();fi.close();oi.close();Goods goods=new Goods();goods.setName(name.getText());goods.setMount(mount.getText()); goods.setPrice(price.getText());goodsList.add(goods);FileOutputStream fo=new FileOutputStream(f);ObjectOutputStream out=new ObjectOutputStream(fo);out.writeObject(goodsList);out.close();}catch(Exception ee) {}}else{try{ f.createNewFile();Goods goods=new Goods();goods.setName(name.getText());goods.setMount(mount.getText()); goods.setPrice(price.getText());goodsList.add(goods);FileOutputStream fo=new  FileOutputStream(f);ObjectOutputStream out=new ObjectOutputStream(fo);out.writeObject(goodsList);out.close();}catch(Exception ee) {}}}
}

WindowsGoods.java

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class WindowGoods extends JFrame implements ActionListener {File file=null;JMenuBar bar;JMenu fileMenu;JMenuItem 录入,显示;JTextArea show;InputArea inputMessage;JPanel pCenter;JTable table;Object 表格单元[][],列名[]={"名称","库存","单价"};CardLayout card;WindowGoods() {file=new File("库存.txt"); //存放链表的文件录入=new JMenuItem("录入");显示=new JMenuItem("显示");bar=new JMenuBar();fileMenu=new JMenu("菜单选项");fileMenu.add(录入);fileMenu.add(显示);bar.add(fileMenu);setJMenuBar(bar);录入.addActionListener(this);显示.addActionListener(this);inputMessage=new InputArea(file); //创建录入截面card=new CardLayout();pCenter=new JPanel();pCenter.setLayout(card);pCenter.add("录入",inputMessage);add(pCenter,BorderLayout.CENTER);setVisible(true);setBounds(100,50,420,380);validate();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public void actionPerformed(ActionEvent e) {if(e.getSource()==录入) {card.show(pCenter,"录入");}else if(e.getSource()==显示) {try{ FileInputStream fi=new FileInputStream(file);ObjectInputStream oi=new  ObjectInputStream(fi);LinkedList<Goods> goodsList=(LinkedList<Goods>)oi.readObject();fi.close();oi.close();  int length=goodsList.size();表格单元=new Object[length][3];table=new JTable(表格单元,列名); pCenter.removeAll(); pCenter.add("录入",inputMessage);pCenter.add("显示",new JScrollPane(table)); pCenter.validate();      Iterator<Goods> iter=goodsList.iterator();int i=0;while(iter.hasNext()) {Goods  商品 =iter.next();表格单元[i][0]= 商品.getName();表格单元[i][1]=商品.getMount();表格单元[i][2]=商品.getPrice();i++;}table.repaint();}catch(Exception ee){}card.show(pCenter,"显示");}}
}

第十五章课后编程题

3.编程题

1.使用堆栈结构输出an的若干项,其中an=2an-1+2an-2,a1=3,a2=8。
2.编写一个程序,将链表中的学生英语成绩单存放到一个树集中,使得按成绩自动排序,并输出排序结果。
3.有10个U盘,有两个重要的属性:价格和容量。编写一个应用程序,使用TreeMap<K,V>类,分别按照价格和容量排序输出10个U盘的详细信息。

3-1

代码链接

https://gitee.com/BESTI-IS-JAVA-2018/20165305zhenlong/blob/master/java/XiTi/XiTi1.java

结果截图

3-2

代码链接

https://gitee.com/BESTI-IS-JAVA-2018/20165305zhenlong/blob/master/java/XiTi/XiTi2.java

结果截图

3-3

代码链接

https://gitee.com/BESTI-IS-JAVA-2018/20165305zhenlong/blob/master/java/XiTi/XiTi3.java

结果截图

转载于:https://www.cnblogs.com/2332175994-1314/p/9000323.html

20165305 第十周课下补做相关推荐

  1. 20165301第十周课下补做

    第十周课下补做 知识点总结 创建一个空的链表 List<Student> list = new LinkedList<Student>(); 向链表中添加新的结点 list.a ...

  2. 20165235 第十周课下补做

    20165235 祁瑛 第十周课下补做 相关知识点的总结 LinkedList<String> mylist=new LinkedList<String>()来创建一个链表. ...

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

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

  4. 20165315 第八周考试课下补做

    20165315 第八周考试课下补做 测试-2-ch03 1.通过输入gcc -S -o main.s main.c 将下面c程序"week0303学号.c"编译成汇编代码 int ...

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

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

  6. 20165332第八周课下作业

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

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

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

  8. 20165320 第十周课上测试补做

    相关知识点总结: 1.LinkedList泛型类创建一个链表对象 LinkedList<String> mylist = new LinkedList<String>(); 2 ...

  9. 20162309单步追踪数组及查找课下补分博客

    课上实验要求:1 用JDB或IDEA单步跟踪在下列数据中(3 8 12 34 54 84 91 110)查找45和54的过程,对比使用顺序查找和二分查找的执行过程. 2提交测试找到或找不到那一步的截图 ...

最新文章

  1. Nginx 动静态访问分离
  2. LibSVM for Python 使用
  3. Tomcat-简易使用教程
  4. Java设计模式-Proxy代理模式
  5. 20170914-构建之法:现代软件工程-阅读笔记
  6. 自动undo管理下如何添加和删除回滚段
  7. 学习笔记(59):Python实战编程-Graphics
  8. 快餐文化短视频源码行业竞争激烈,短视频发展任重道远
  9. 分包组包 北斗通信_蓝牙mesh底层传输层(分包和组包)
  10. Python提取彩色图像的二值化边缘
  11. java中InvocationHandler 用于实现代理。
  12. 我整理的一份来自于线上的Nginx配置(Nginx.conf),希望对学习Nginx的有帮助
  13. LOJ#6038. 「雅礼集训 2017 Day5」远行(LCT)
  14. 新型养老机构的“智安”神器 | ​钛颐康智慧养老物联网平台
  15. 目前人工智能应用领域比较多,主要包含哪些应用领域?
  16. VirtualBox 无法安装 已删除了注册表及文件
  17. Java 算法之三色旗
  18. ClassNotFoundException,某种类的缺失
  19. 7 种模型加权集成方法
  20. 关于antV G6中的on事件、util.each事件及update方法等的使用总结

热门文章

  1. css3:border-radius圆角边框详解 (变圆 图片)
  2. GPU版本的Keas安装
  3. MATLAB系统辨识工具箱学习
  4. php web 简单的系统,Slim 简单而强大的 PHP 微型 Web 开发框架 - 资源分享
  5. kafka使用_Kafka精华问答 | kafka的使用场景是什么?
  6. 波卡生态预言机ROOM上线Uniswap,最高涨幅达862%
  7. SAP License:财务报表版本文本
  8. SAP License:2021年最新FICO面试答案
  9. SAP License:SAP系统中的删除命令
  10. SAP License:SAP顾问是如何炼成的——SAP顾问的真实生活