Java學習筆記-集合框架中的接口

          Posted on 2006-06-27 17:36 多力宇揚 閱讀(242) 評論(0)  編輯  收藏 所屬分類: Core Java

          ?Collection?????????????Map(和Collection接口沒任何關系)
          ?????? / \???????????????????????? |
          ????? /?? \??????????????????????? |??????
          ?Set?? List??????????????SortedMap
          ??? /
          ?? /
          SortedSet
          (接口圖)

          所謂框架就是一個類庫的集合。集合框架就是一個用來表示和操作集合的統一框架,包含了實現
          集合的接口和類。
          ?
          ?集合框架中的接口

          ?.Collection: 集合層次中的根接口,JDK沒有提供這個接口直接的實現類。
          ?.Set: 不能包含重復的元素。SortedSet是一個按照升序排列元素的Set。
          ?.List: 是一個有序的集合,可以包含重復的元素。提供了按照索引訪問的方式。
          ?.Map: 包含了key-value對。Map不能包含重復的key。SortedMap是一個按照升序排列key的Map。
          ?
          ?集合框架中的實現類
          ?
          ?實線表示繼承類,虛線表示實現類。
          ?(圖如下)

          .ArrayList: 我們可以將其看做是能夠自動增長容量的數組。
          ?.利用ArrayList的toArray()返回一個數組。
          ?.Arrays.asList()返回一個列表。
          ?.迭代器(Iterator)給我們提供了一種通用的方式來訪問集合中的元素。

          ?注意: 從集合類中獲取一個數組 toArray(),從數組獲取列表利用Arrays.asList()
          ?例子:
          import java.util.*;
          class ArrayListTest
          {
          ?public static void printElement(Collection c)
          ?{
          ? Iterator it = c.iterator();
          ? while(it.hasNext())
          ? {
          ?? System.out.println(it.next());
          ? }
          ?}
          ?public static void main(String[] args)
          ?{
          ? ArrayList a = new ArrayList();
          ? /*
          ? a.add("abc");
          ? a.add("def");
          ? a.add("hjk");
          ? */
          ?
          ? a.add(new Point(1,1));
          ? a.add(new Point(2,2));
          ? a.add(new Point(3,3));
          ? /*
          ? Object[] o;
          ? o=a.toArray();? //將集合類轉換為數組
          ? for(int i=0;i<o.length;i++)
          ? {
          ?? System.out.println(o[i]);
          ? }
          ?
          ? List b = new ArrayList();
          ? b=Arrays.asList(o);
          ? System.out.println(b);
          ?
          ? for(int i=0;i<a.size();i++)
          ? {
          ?? System.out.println(a.get(i));
          ? }
          ?
          ? System.out.println(a);
          ? System.out.println(a);
          ? */
          ?
          ? /*
          ? Iterator it = a.iterator();
          ?? 如果要刪除元素,必須先調用next方法
          ? it.next();
          ? it.remove();
          ? while(it.hasNext())
          ? {
          ?? System.out.println(it.next());
          ? }
          ? */
          ? //迭代器的作用: 提供一組通用的訪問方式
          ? printElement(a);
          ?}
          }

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

          ?Collections類

          ?.排序: Collections.sort(); [區別與Arrays.sort()]
          ? (1) 自然排序(natural ordering);
          ? (2) 實現比較器(Comparator)接口。
          ?.取最大和最小的元素: Collections.max(),Collections.min();
          ?.在已排序的List中搜索指定的元素: Collections.binarySearch()。

          ?代碼示例:
          import java.util.*;
          class ArrayListTest
          {
          ?public static void printElement(Collection c)
          ?{
          ? Iterator it = c.iterator();
          ? while(it.hasNext())
          ? {
          ?? System.out.println(it.next());
          ? }
          ?}
          ?public static void main(String[] args)
          ?{
          ? /*
          ? ArrayList a = new ArrayList();
          ?
          ? a.add("abc");
          ? a.add("def");
          ? a.add("hjk");
          ?
          ?
          ? a.add(new Point(1,1));
          ? a.add(new Point(2,2));
          ? a.add(new Point(3,3));
          ?
          ? Object[] o;
          ? o=a.toArray();? //將集合類轉換為數組
          ? for(int i=0;i<o.length;i++)
          ? {
          ?? System.out.println(o[i]);
          ? }
          ?
          ? List b = new ArrayList();
          ? b=Arrays.asList(o);
          ? System.out.println(b);
          ?
          ? for(int i=0;i<a.size();i++)
          ? {
          ?? System.out.println(a.get(i));
          ? }
          ?
          ? System.out.println(a);
          ? System.out.println(a);
          ? */
          ?
          ? /*
          ? Iterator it = a.iterator();
          ?? 如果要刪除元素,必須先調用next方法
          ? it.next();
          ? it.remove();
          ? while(it.hasNext())
          ? {
          ?? System.out.println(it.next());
          ? }
          ? */
          ? //迭代器的作用: 提供一組通用的訪問方式
          ? //printElement(a);
          ?
          ? Student s1 = new Student(1,"zhangsan");
          ? Student s2 = new Student(2,"lisi");
          ? Student s3 = new Student(3,"wangwu");
          ? Student s4 = new Student(3,"blovesaga");
          ?
          ? ArrayList a = new ArrayList();
          ? a.add(s1);
          ? a.add(s2);
          ? a.add(s3);
          ? a.add(s4);
          ?
          ? //Collections.sort(a);
          ? Collections.sort(a,new Student.StudentComparator());
          ? printElement(a);
          ?}
          }

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

          class Student implements Comparable
          {
          ?int num;
          ?String name;
          ?//實現比較器,它總是和我們的一個類相關,用內部類
          ?static class StudentComparator implements Comparator? //為了調用方便聲明為靜態的
          ?{
          ? public int compare(Object o1,Object o2)
          ? {
          ?? Student s1 = (Student)o1;
          ?? Student s2 = (Student)o2;
          ?? int result=s1.num > s2.num ? 1: (s1.num==s2.num ? 0 : -1);
          ?? if(result==0)
          ?? {
          ??? return s1.name.compareTo(s2.name);
          ?? }
          ?? return result;
          ? }
          ?}
          ?Student(int num,String name)
          ?{
          ? this.num=num;
          ? this.name=name;
          ?}
          ?public int compareTo(Object o)
          ?{
          ? Student s=(Student)o;
          ? return num > s.num ? 1 :( (num==s.num) ? 0 : -1);
          ?}
          ?
          ?public String toString()
          ?{
          ? return +num+":"+name;
          ?}
          }

          主站蜘蛛池模板: 云和县| 密山市| 彝良县| 周宁县| 黔南| 阿荣旗| 平定县| 化德县| 伊金霍洛旗| 得荣县| 思茅市| 宿迁市| 元江| 专栏| 老河口市| 怀远县| 琼结县| 常州市| 商洛市| 甘南县| 玛纳斯县| 鱼台县| 久治县| 调兵山市| 锡林浩特市| 威信县| 抚远县| 淅川县| 景洪市| 雷波县| 澄城县| 谢通门县| 兴城市| 新宁县| 米泉市| 湄潭县| 许昌市| 临清市| 邳州市| 建平县| 堆龙德庆县|