教材p448 Example15_4java
刻下推送代碼到碼雲node
將代碼簡單修改學號姓名就好
截圖以下:
算法
數據結構和算法中,排序是很重要的操做,要讓一個類能夠進行排序,有兩種方法:編程
針對下面的Student類,使用Comparator編程完成如下功能:數組
comparator接口(01,02)->{}數據結構
排序默認是按照升序排序
若是返回-1,就認爲01 小於02,(注意01和02的順序)
若是返回0,認爲兩個相等
若是返回1,就認爲01大於02,(注意01和02的順序)
對student對象數組進行排序,用的sort方法,在實現comparator接口時,sort方法須要傳進來兩個參數,即stu對象數組,以及重寫的實現了comparator比較方法類。
代碼以下:數據結構和算法
import java.util.*; class StudentData { public static void main(String[] args) { List<Student> list = new LinkedList<>(); list.add(new Student(20155327,"李百乾","m",21,66,88,89)); list.add(new Student(20165328,"張三","m",21,77,99,76)); list.add(new Student(20165329,"李四","w",20,88,77,78)); list.add(new Student(20165330,"王五","m",21,99,87,90)); list.add(new Student(20165331,"週六","m",20,98,78,93)); SortByTotal_score sortBytotal_score = new SortByTotal_score(); Collections.sort(list, sortBytotal_score); SortByID sortByID = new SortByID(); 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 "Student[name:"+name+",age:"+age+",number:"+id+",total_score"+total_score+"]"; } public Student(int id, String name, String sex, int age, double computer_score, double english_score, double maths_score) { this.id = id; this.name = name; this.sex = sex; this.age = age; this.computer_score = computer_score; this.english_score = english_score; this.maths_score = maths_score; } public String getName() { return name; } 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 setName(String name) { this.name = name; } 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 SortByID implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return o1.getId() - o2.getId(); } } class SortByTotal_score implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return (int)( o1.getTotalScore() - o2.getTotalScore()); } }
截圖以下
ide
參見附件,補充MyList.java的內容,提交運行結果截圖(全屏)
課下推送代碼到碼雲測試
1.編寫一個Node類來充當結點的模型。咱們知道,其中有兩個屬性,1存放數據的data,2存放下一結點的引用this
public class Node<T> //單鏈表結點類,T指定結點的元素類型 { public T data; //數據域,存儲數據元素 public Node<T> next; //地址域,引用後繼結點 public Node(T data, Node<T> next) //構造結點,data指定數據元素,next指定後繼結點 { this.data = data; //T對象引用賦值 this.next = next; //Node<T>對象引用賦值 } public Node() { this(null, null); } @Override public String toString() //返回結點數據域的描述字符串 { return this.data.toString(); } }
2.單鏈表的簡單操做(增長,刪除,獲取總長度,鏈表元素排序,鏈表遍歷):
增長結點操做,addNode(Node):經過移動的指針遍歷整個鏈表,找到最後一個結點,日後添加便可
插入結點到鏈表的指定位置。 insertNodeByIndex(int index,Node node)
刪除指定位置上的結點 delNodeByIndex(int index)
代碼以下:
/** * Created by lz50 on 2018/6/15. */ public class MyList { public static void main(String[] args) { //選用合適的構造方法,用你學號先後各兩名同窗的學號建立四個結點 Node<Integer> S1 = new Node<Integer>(20155325, null); Node<Integer> S2 = new Node<Integer>(20155326, null); Node<Integer> S3 = new Node<Integer>(20155328, null); Node<Integer> S4 = new Node<Integer>(20155329, null); //把上面四個節點連成一個沒有頭結點的單鏈表 S1.next = S2; S2.next = S3; S3.next = S4; //遍歷單鏈表,打印每一個結點的 Node<Integer> s = S1; while (s != null) { System.out.println(s.data); s = s.next; } System.out.println(); //把你本身插入到合適的位置(學號升序) Node<Integer> M = new Node<Integer>(20155327, null); s = S1; while (s != null) { if (s.data < 20155327 && s.next.data > 20155327) { M.next = s.next; s.next = M; break; } else { s = s.next; } } System.out.println(); //遍歷單鏈表,打印每一個結點的 s = S1; while (s != null) { System.out.println(s.data); s = s.next; } System.out.println(); //從鏈表中刪除本身 s = S1; while (s != null) { if (s.next.data == 20155327) { s.next = s.next.next; break; } else { s = s.next; } } System.out.println(); //遍歷單鏈表,打印每一個結點的 s = S1; while (s != null) { System.out.println(s.data); s = s.next; } } }
截圖以下: