第十周課下作業
阿新 • • 發佈:2018-05-05
ear object lec 對象 for 構造方法 class sys bubuko
第十周課下作業(補做課堂測試)
一、知識點總結
1、單鏈表
- 創建單鏈表
- 鏈表中數據的插入
list.add("**");
- 鏈表中數據的排序
Collections.sort();
- 鏈表中數據的刪除
lsit.remove("");
2、排序
- 樹集概念
- 樹映射
TreeMap<K,V>**適合用於數據的排序**
- 通過關鍵字進行排序
TreeMap<StudentKey,Student> treemap= new TreeMap<StudentKey,Student>();
二、補做內容與截圖
1、單鏈表
創建鏈表
創建結點
插入自己的學號並排序
刪除自己的學號並打印
整體代碼
import java.util.*; public class MyList { public static void main(String [] args) { List<String> mylist=new LinkedList<String>(); //選用合適的構造方法,用你學號前後各兩名同學的學號創建四個結點 mylist.add("20165224"); mylist.add("20165225"); mylist.add("20165227"); mylist.add("20165228"); //把上面四個節點連成一個沒有頭結點的單鏈表 System.out.println("打印初始鏈表"); //遍歷單鏈表,打印每個結點的 Iterator<String> iter=mylist.iterator(); while (iter.hasNext()){ String num=iter.next(); System.out.println(num); } //把你自己插入到合適的位置(學號升序) mylist.add("20165226"); Collections.sort(mylist); //遍歷單鏈表,打印每個結點的 System.out.println("插入我的學號在排序之後,鏈表中的數據:"); iter =mylist.iterator(); while(iter.hasNext()){ String num=iter.next(); System.out.println(num); } //從鏈表中刪除自己 mylist.remove("20165226"); System.out.println("刪除我的學號之後打印鏈表:"); //遍歷單鏈表,打印每個結點的 iter=mylist.iterator(); while(iter.hasNext()){ String num=iter.next(); System.out.println(num); } } }
運行結果
2、排序
創建
Student
類的對象
運用TreeMap排序方法
按關鍵字
總成績
進行排序
按關鍵字
學號
進行排序
運行結果
三、補做教材第十五章編程題目
1、使用堆棧結構輸出an的若幹項,其中an=2an-1 +2an-2 ,a1=3,a2=8
import java.util.*; public class E { 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++; } } } }
2、編寫一個程序,將鏈表中的學生英語成績單存放到一個樹集中,使得按成績自動排序,並輸出排序結果
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 E {
public static void main(String args[]) {
List<Student> list=new LinkedList<Student>();
int score []={65,76,45,99,77,88,100,79};
String name[]={"張三","李四","旺季","加戈","為哈","周和","趙李","將集"};
for(int i=0;i<score.length;i++){
list.add(new Student(score[i],name[i]));
}
Iterator<Student> iter=list.iterator();
TreeSet<Student> mytree=new TreeSet<Student>();
while(iter.hasNext()){
Student stu=iter.next();
mytree.add(stu);
}
Iterator<Student> te=mytree.iterator();
while(te.hasNext()) {
Student stu=te.next();
System.out.println(""+stu.name+" "+stu.english);
}
}
}
3、有10個U盤,有兩個重要的屬性:價格和容量。編寫一個應用程序,使用TreeMap
import java.util.*;
class UDiscKey implements Comparable {
double key=0;
UDiscKey(double d) {
key=d;
}
public int compareTo(Object b) {
UDiscKey disc=(UDiscKey)b;
if((this.key-disc.key)==0)
return -1;
else
return (int)((this.key-disc.key)*1000);
}
}
class UDisc{
int amount;
double price;
UDisc(int m,double e) {
amount=m;
price=e;
}
}
public class E {
public static void main(String args[ ]) {
TreeMap<UDiscKey,UDisc> treemap= new TreeMap<UDiscKey,UDisc>();
int amount[]={1,2,4,8,16};
double price[]={867,266,390,556};
UDisc UDisc[]=new UDisc[4];
for(int k=0;k<UDisc.length;k++) {
UDisc[k]=new UDisc(amount[k],price[k]);
}
UDiscKey key[]=new UDiscKey[4];
for(int k=0;k<key.length;k++) {
key[k]=new UDiscKey(UDisc[k].amount); }
for(int k=0;k<UDisc.length;k++) {
treemap.put(key[k],UDisc[k]); }
int number=treemap.size();
Collection<UDisc> collection=treemap.values();
Iterator<UDisc> iter=collection.iterator();
while(iter.hasNext()) {
UDisc disc=iter.next();
System.out.println(""+disc.amount+"G "+disc.price+"元"); }
treemap.clear();
for(int k=0;k<key.length;k++) {
key[k]=new UDiscKey(UDisc[k].price); }
for(int k=0;k<UDisc.length;k++) {
treemap.put(key[k],UDisc[k]); }
number=treemap.size();
collection=treemap.values();
iter=collection.iterator();
while(iter.hasNext()) {
UDisc disc=iter.next();
System.out.println(""+disc.amount+"G "+disc.price+"元");
}
}
}
第十周課下作業