所謂框架就是一個類庫的集合。集合框架就是一個用來表示和操作集合的同意架構,包含了實現集合的接口和類。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
?* 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;
?}
}