20165235 Qi Ying's tenth week make-up after class
Summary of relevant knowledge points
LinkedList<String> mylist=new LinkedList<String>()
to create a linked list.
mylist.add();
to add nodes.
get(int index)
to get the object of the node at the index-th position in the linked list.
public static sort(List<E>)
Arrange the elements in the linked list in ascending order
public static binarySearch(List<T>,T key,CompareTo<T>c)
: use half to find the data in the list Key
;
implement the Comparable
interface to override the compareTo
method. The methods in Collections
the class are designed for the interface.sort
Comparable
Make up the content of the class, screenshot of the result
Data Structure - Sorting
import java.util.*;
public class Example {
public static void main(String[] args) {
List<Student> list = new LinkedList<>();
list.add(new Student(20165233,"张雨昕",99,91,89));
list.add(new Student(20165234,"刘京甫",76,66,95));
list.add(new Student(20165235,"祁 瑛",77,81,68));
list.add(new Student(20165236,"郭金涛",89,45,66));
list.add(new Student(20165237,"方若鸿",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; //表示平均成绩
@Override
public 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> {
@Override
public int compare(Student a, Student b) {
return a.getId() - b.getId();
}
}
class SortTotal_score implements Comparator<Student> {
@Override
public int compare(Student a, Student b) {
return (int)( a.getTotalScore() - b.getTotalScore());
}
}
-
data structure singly linked list
import java.util.*; public class Mylist { public static void main(String [] args) { //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点 Node<String> node1 =new Node<String>("20165233",null); Node<String> node2 =new Node<String>("20165234",null); Node<String> node3 =new Node<String>("20165236",null); Node<String> node4 =new Node<String>("20165237",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>("20165235",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); } @Override public java.lang.String toString() //返回结点数据域的描述字符串 { return this.data.toString(); } }
-
Code Analysis in Chapter 15 of the textbook
import java.util.*;
public class Example15_2 {
public static void main(String args[]){
List<String> list=new LinkedList<String>();//建立一个String类型的链表
for(int i=0;i<=60096;i++){
list.add("speed"+i);//对链表插入节点
}
Iterator<String> iter=list.iterator();//用iterator()方法获得一个Iterator对象
long starttime=System.currentTimeMillis();
while(iter.hasNext()){//当链表中还有数据时返回true
String te=iter.next();//te指向下一个节点
}
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 { //实现Comparab接口
int height=0;
String name;
Student(String n,int h) {
name=n;
height = h;
}
public int compareTo(Object b) { // 重写接口中的compareTo方法,两个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);//使用Collections类的静态方法sort来升序排序
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);//使用Collections中的静态方法binarySearch()方法进行折半查找
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>//建立一个int类型的链表
for(int i=10;i<=50;i=i+10)//添加5个节点
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);//将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);//翻转list中的数据
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>();//建立一个int类型的堆栈
stack.push(new Integer(1)); //将1压栈
stack.push(new Integer(1));//将1压栈,现在栈中两个数
int k=1;
while(k<=10) {//求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) {//重写compareTo方法
Student st=(Student)b;//Object类型强制转化为Student类
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.*;
class StudentKey implements Comparable {
double d=0;
StudentKey (double d) {
this.d=d;
}
public int compareTo(Object b) {//重写compareTo方法
StudentKey st=(StudentKey)b;//强制转化为StudentKey类型
if((this.d-st.d)==0)
return -1;
else
return (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>();
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();//获取treemap的元素个数
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);
}
}
}
Make up for the programming topics in Chapter 15 of the textbook
Programming Question 1
import java.util.*;
public class Series {
public static void main(String args[]){
Stack<Integer> stack =new Stack<Integer>();
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int a1=3;
int a2=8;
stack.push(a1);//第一个数压栈
stack.push(a2);//第二个数压栈
int temp=0;//计算结果设一个中间变量temp
for(int i=0;i<n;i++){
a2=stack.pop();//弹出第二个数
a1=stack.pop();//弹出第一个数
temp=2*a2+2*a1;//计算结果
stack.push(a2);//第二个数压栈
stack.push(temp);//计算结果压栈
System.out.println(temp);
}
}
}
- Code cloud link
Programming Question 2
import java.util.*;
public class SortEnglish {
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;
}
@Override
public int compareTo(Object a) {
Score b = (Score) a;
return this.english - b.english;
}
}
Programming Question 3
- Code cloud link
import java.util.*;
class Sort implements Comparable {
double d=0;
Sort (double d) {
this.d=d;
}
@Override
public 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);
}
}
}
-
experience
In this test, I have not been able to do sorting and singly linked list questions. One is that the test time is too short, and the other is that I have not learned enough about the data structure. And through this class, my understanding of the data structure has been further improved.