AntSoul

          它總是在行走,行走,永遠的行走…… 行走是它生存的恒久姿態和最佳造型。 它似乎有一雙不知疲倦的腳。 ———我說的是螞蟻。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            42 隨筆 :: 0 文章 :: 1 評論 :: 0 Trackbacks

          所謂框架就是一個類庫的集合。集合框架就是一個用來表示和操作集合的同意架構,包含了實現集合的接口和類。Java中的集合框架結構圖如下:

          ??????? Collection?(i)?????????????? Map(i)
          ??????????? /???? \????????????????????????????? |
          ????????? /???????? \??????????????????????????? |
          ?????? Set(i)?? List(i)???????????????SortedMap(i)
          ????????/
          ????SortedSet(i)
          區別:
          Collection: 集合層次中的根接口。
          Set: 不能包含重復的element。SortedSet按照升序排列elements的Set。
          List: 有序(不是排序,而是指elements按照一定的順序排列),可以包含重復element,提供了索引訪問的方式。

          △ArrayList
          1) ?ArrayList?我們可以看作是一個可以自動增長的數組,這是和數組的不同之處。
          2)? 利用ArrayList的toArray()方法返回一個對象數組。
          3)? Arrays的asList()返回一個列表。 注:?返回固定尺寸的列表。asList()返回的列表不支持remove()方法。
          4)? 迭代器(Iterator)提供了一組訪問集合的通用方法。 hasNext(),? next(),?? remove(),調用remove()方法之前必須至少調用一次next().
          5)??類Collections與Arrays,前者對列表排序,后者對數組排序。
          ?* 當我們在打印一個集合類的對象的時候,它會調用集合類中的toString()方法,所以我們自定義的類就必須重寫toString()方法.
          ?* List stooges = Arrays.asList("Larry", "Moe", "Curly");
          ?* public static void printElements(Collecion c){
          ??????? Iterator it = c.iterator();
          ???????? while(it.hasNext()){
          ???????????? System.out.println(it.next());
          ???????? }
          ??? }

          ?
          ?? △ Collections
          ?? 1) 排序Collections.sort()
          ??????? a. 自然排序(natural ordering);
          ??????? b. 實現比較器(Comparator)接口.
          ?? 2)? 取最大最element: Collections.max();??Collections.min();
          ?? 3)? 在已經排序的List中搜索指定的element:? Collections.binarySerach()。


          ???△? 一般方法實現
          ?? import java.util.*;

          class ArrayListTest
          {
          ?public static void printElements(Collection c){
          ??Iterator it = c.iterator();
          ??while(it.hasNext()){
          ???System.out.println(it.next());
          ??}
          ?}
          ?
          ?public static void main(String[] args){
          ?? Student s1 = new Student("antsoul",25);
          ?? Student s2 = new Student("feiyang",35);
          ?? Student s3 = new Student("gll",24);
          ?? Student s4 = new Student("andylau",40);
          ??
          ?? ArrayList al = new ArrayList();
          ?? al.add(s1);
          ?? al.add(s2);
          ?? al.add(s3);
          ?? al.add(s4);
          ??
          ?? Collections.sort(al);
          ?? printElements(al);
          ?}
          }

          class Point
          {
          ?int x,y;
          ?public Point(int x,int y){
          ??this.x = x;
          ??this.y = y;
          ?}
          ?
          ?public String toString(){
          ??return ("x="+x+","+"y="+y);
          ?}
          }

          class Student implements Comparable
          {
          ?private String name;
          ?private int num;
          ?
          ?public Student(String name,int num){
          ??this.name = name;
          ??this.num = num;
          ?}
          ?
          ?public int compareTo(Object o){
          ??Student s =(Student)o;
          ??return num > s.num ? 1 : (num == s.num ? 0 : -1);
          ?}
          ?
          ?public String toString(){
          ??return "num:"+num+" "+"name:"+name;
          ?}
          }????

          ?? △ 比較器總是和特定的類相關的,具體到某一個類。比如說對student排序,你要用到學號,所以排序前必須要轉換Object為Student,也就是為某一個類指定一個比較器,可以寫一個類去實現比較器的接口,但是為了聯系緊密,可以在這里用內部類在實現比機器接口。
          import java.util.*;

          class ArrayListTest
          {
          ?public static void printElements(Collection c){
          ??Iterator it = c.iterator();
          ??while(it.hasNext()){
          ???System.out.println(it.next());
          ??}
          ?}
          ?
          ?public static void main(String[] args){
          ?? Student s1 = new Student("antsoul",2);
          ?? Student s2 = new Student("feiyang",1);
          ?? Student s3 = new Student("gll",3);
          ?? Student s4 = new Student("andylau",4);
          ??
          ?? ArrayList al = new ArrayList();
          ?? al.add(s1);
          ?? al.add(s2);
          ?? al.add(s3);
          ?? al.add(s4);
          ??
          ?? Collections.sort(al,new Student.StudentComparator()); //student提供自己的比較器
          ?? printElements(al);
          ?}
          }

          class Point
          {
          ?int x,y;
          ?public Point(int x,int y){
          ??this.x = x;
          ??this.y = y;
          ?}
          ?
          ?public String toString(){
          ??return ("x="+x+","+"y="+y);
          ?}
          }

          class Student implements Comparable
          {
          ?private String name;
          ?private int num;
          ?
          ?//為了調用方便聲明為static
          ?static class StudentComparator implements Comparator
          ?{
          ?? public int compare(Object o1,Object o2){
          ??? Student s1 =(Student)o1;
          ??? Student s2 =(Student)o2;
          ???
          ??? return s1.num > s2.num ? 1 :(s1.num==s2.num ? 0 : -1);
          ?? }?
          ?}
          ?
          ?public Student(String name,int num){
          ??this.name = name;
          ??this.num = num;
          ?}
          ?
          ?public int compareTo(Object o){
          ??Student s =(Student)o;
          ??return num > s.num ? 1 : (num == s.num ? 0 : -1);
          ?}
          ?
          ?public String toString(){
          ??return "num:"+num+" "+"name:"+name;
          ?}
          }

          如果student的num相等的情況下,要以name來排序可以這樣實現:
          import java.util.*;

          class ArrayListTest
          {
          ?public static void printElements(Collection c){
          ??Iterator it = c.iterator();
          ??while(it.hasNext()){
          ???System.out.println(it.next());
          ??}
          ?}
          ?
          ?public static void main(String[] args){
          ?? Student s1 = new Student("antsoul",2);
          ?? Student s2 = new Student("feiyang",1);
          ?? Student s3 = new Student("dorydoo",3);
          ?? Student s4 = new Student("sun",4);
          ?? Student s5 = new Student("gll",4);
          ??
          ?? ArrayList al = new ArrayList();
          ?? al.add(s1);
          ?? al.add(s2);
          ?? al.add(s3);
          ?? al.add(s4);
          ?? al.add(s5);
          ??
          ?? Collections.sort(al,new Student.StudentComparator()); //student提供自己的比較器
          ?? printElements(al);
          ?}
          }

          class Point
          {
          ?int x,y;
          ?public Point(int x,int y){
          ??this.x = x;
          ??this.y = y;
          ?}
          ?
          ?public String toString(){
          ??return ("x="+x+","+"y="+y);
          ?}
          }

          class Student implements Comparable
          {
          ?private String name;
          ?private int num;
          ?
          ?//為了調用方便聲明為static
          ?static class StudentComparator implements Comparator
          ?{
          ?? public int compare(Object o1,Object o2){
          ??? Student s1 =(Student)o1;
          ??? Student s2 =(Student)o2;
          ??? int result;
          ???
          ??? result = s1.num > s2.num ? 1 :(s1.num==s2.num ? 0 : -1);
          ??? if(result == 0){ //student的num相同,比較name,因為name為String類型,它實現了Comparable<String>
          ??? ? result = s1.name.compareTo(s2.name);
          ??? }
          ??? return result;
          ?? }?
          ?}
          ?
          ?public Student(String name,int num){
          ??this.name = name;
          ??this.num = num;
          ?}
          ?
          ?public int compareTo(Object o){
          ??Student s =(Student)o;
          ??return num > s.num ? 1 : (num == s.num ? 0 : -1);
          ?}
          ?
          ?public String toString(){
          ??return "num="+num+" "+"name="+name;
          ?}
          }




          posted on 2007-03-10 13:03 yok 閱讀(205) 評論(0)  編輯  收藏 所屬分類: CoreJava
          主站蜘蛛池模板: 汝阳县| 建平县| 大田县| 盱眙县| 巫溪县| 宁晋县| 嘉兴市| 泸定县| 突泉县| 遂溪县| 互助| 淮南市| 龙门县| 黄大仙区| 柘荣县| 北安市| 德州市| 南召县| 普安县| 楚雄市| 南岸区| 遂宁市| 蓬莱市| 福建省| 阿城市| 青海省| 永顺县| 彰武县| 宜川县| 兴国县| 玛纳斯县| 枣阳市| 大同县| 长顺县| 海盐县| 巫溪县| 红桥区| 南丰县| 望谟县| 台北县| 晋城|