20165235 第十周課下補做
阿新 • • 發佈:2018-05-06
tps tee private 同學 ID 結果 void d+ commit 接口要重寫
20165235 祁瑛 第十周課下補做
相關知識點的總結
LinkedList<String> mylist=new LinkedList<String>()
來創建一個鏈表。
mylist.add();
來添加結點。
get(int index)
來獲取鏈表中第index個位置的結點的對象。
public static sort(List<E>)
將鏈表中的元素升序排列
public static binarySearch(List<T>,T key,CompareTo<T>c)
:使用折半查找list中的數據Key
;
實現Comparable
compareTo
方法。Collections
類中的sort
方法是面向Comparable
接口設計的。
課上內容的補做,結果截圖
數據結構-排序
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()); } }
碼雲鏈接
數據結構單鏈表
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(); } }
碼雲鏈接
教材第十五章的代碼分析
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);
}
}
}
補做教材第十五章的編程題目
編程題一
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);
}
}
}
- 碼雲鏈接
編程題二
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;
}
}
編程題三
- 碼雲鏈接
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);
}
}
}
碼雲鏈接
心得體會
本次測試我沒有能夠做出排序和單鏈表的題目,一是考試時間過短,二是對數據結構這塊內容學習的不夠深。而通過本次課下補做我對數據結構的了解有進了一步。
20165235 第十周課下補做