list.add("**");
Collections.sort();
lsit.remove("");
TreeMap<K,V>**適合用於數據的排序**
TreeMap<StudentKey,Student> treemap= new TreeMap<StudentKey,Student>();
在List或數組中的對象若是沒有實現Comparable接口時,那麼就須要調用者爲須要排序的數組或List設置一個Compartor,Compartor的compare方法用來告訴代碼應該怎麼去比較兩個實例,而後根據比較結果進行排序
- comparator
package java.util; public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); }
- comparable
package java.lang; import java.util.*; public interface Comparable<T> { public int compareTo(T o); }
- 總結
Comparable 是排序接口;若一個類實現了 Comparable 接口,就意味着 「該類支持排序」。而 Comparator 是比較器;咱們若須要控制某個類的次序,能夠創建一個 「該類的比較器」 來進行排序。 前者應該比較固定,和一個具體類相綁定,然後者比較靈活,它能夠被用於各個須要比較功能的類使用。能夠說前者屬於 「靜態綁定」,然後者能夠 「動態綁定」。 Comparable 至關於 「內部比較器」,而 Comparator 至關於 「外部比較器」。
建立鏈表
java
建立結點
編程
插入本身的學號並排序
數組
刪除本身的學號並打印
ide
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); } } }
建立Student
類的對象
測試
調用comparator方法
this
按關鍵字總成績
進行排序
3d
按關鍵字學號
進行排序
code
import java.lang.String; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class StudentTest { class Stu{ public int age; public String name; public int id; public int english_score; public int computer_score; public int maths_score; public int total_score; public Stu(int id, String name,int english_score,int computer_score,int maths_score,int total_score) { super(); this.id = id; this.name = name; this.english_score = english_score; this.computer_score = computer_score; this.maths_score = maths_score; this.total_score = total_score; } @Override public String toString() { return ( "\n"+" 學號 " + id + " 姓名 " + name +" 英語 "+english_score+" 計算機 "+computer_score+" 數學 "+maths_score+" 總成績 "+total_score+"\n"); } } public static void main(String[] args) { List<Stu> list= new ArrayList<>(); list.add(new StudentTest().new Stu(20165224, "陸藝傑",89,67,78,234)); list.add(new StudentTest().new Stu(20165227, "朱越",78,90,98,266)); list.add(new StudentTest().new Stu(20165225, "王高源",45,66,87,198)); list.add(new StudentTest().new Stu(20165226, "劉香杉",88,90,88,266)); list.add(new StudentTest().new Stu(20165228, "蘇祚堃",76,56,89,221)); Collections.sort(list, new Comparator<Stu>() { @Override public int compare(Stu o1, Stu o2) { return o1.id - o2.id; } }); System.out.println("按照學號排序:"+list); Collections.sort(list, new Comparator<Stu>() { @Override public int compare(Stu o1, Stu o2) { return o1.total_score - o2.total_score; } }); System.out.println("按總成績順序排序:"+list); } }
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++; } } } }
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); } } }
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+"元"); } } }