public class TestList {    public static void main(String agrs[]) {        List l1 = new LinkedList();        for (int i = 0; i <= 5; i++) {            l1.add("a" + i);        }        System.out.PRintln(l1);        l1.add(3, "a100");        System.out.println(l1);        l1.set(6, "a200");        System.out.println(l1);        System.out.print((String) l1.get(2) + " ");        System.out.println(l1.indexOf("a3"));        l1.remove(1);        System.out.println(l1);    }}結果是:
[a0, a1, a2, a3, a4, a5]
[a0, a1, a2, a100, a3, a4, a5][a0, a1, a2, a100, a3, a4, a200]a2   4[a0, a2, a100, a3, a4, a200]?2.List常用算法?類java.util.Collections提供了一些靜態方法實現了基于List容器的一些常用算法。?void sort(List)                             對List容器內的元素排序?void shuffle(List)                        對List容器內的對象進行隨機排序?void reverse(List)                      對List容器內的對象進行逆序排序?void fill(List,Object)                   用一個特定的對象重寫整個List容器?void copy(List dest,List src)    將src List容器內容拷貝到dest List容器?int binarySearch(List,Object)  對于順序的List容器,采用折半查找的方法查找特定對象?算法舉例:public class TestList {    public static void main(String agrs[]) {        List l1 = new LinkedList();        List l2 = new LinkedList();        for (int i = 0; i <= 9; i++) {            l1.add("a" + i);        }        System.out.println(l1);        Collections.shuffle(l1); //隨機排序        System.out.println(l1);        Collections.reverse(l1); //逆序        System.out.println(l1);        Collections.sort(l1); //排序        System.out.println(l1);        System.out.println(Collections.binarySearch(l1, "a5")); //折半查找    }}結果是:[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9][a6, a8, a9, a1, a2, a5, a0, a3, a4, a7][a7, a4, a3, a0, a5, a2, a1, a9, a8, a6][a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]53.Comparable接口?所有可以“排序“的類都實現了java.lang.Comparable接口,comparable接口中只有一個方法public int compareTo(Object obj);該方法:?返回0表示this==obj,正數this>obj,負數this實現了Comparable接口的類通常實現comparaTo方法從而確定該類對象的排序方式。?將上述的Name類改寫:class Name implements Comparable加入public int compareTo(Object o) {Name n = (Name)o;int lastCmp =c.compareTo(n.c);return(lastCmp!=0 ? lastCmp :s.compareTo(n.s));}?然后運行:public class TestComparable {    public static void main(String agrs[]) {        List l1 = new LinkedList();        l1.add(new Name("Karl", "M"));        l1.add(new Name("Steven", "Lee"));        l1.add(new Name("John", "O"));        l1.add(new Name("Tom", "M"));        System.out.println(l1);        Collections.sort(l1);        System.out.println(l1);    }}結果是:[Karl M, Steven Lee, John O, Tom M][Steven Lee, Karl M, Tom M, John O]
?4.如何選擇數據結構?衡量標準:讀的效率和改的效率?Array讀快改慢,Linked改快讀慢,Hash兩者之間?
5.Map接口?實現Map接口的類用來存儲 鍵—值 對。?Map接口的實現類有HashMap(哈希表)和TreeMap(二叉樹)等?。?二叉樹:在計算機科學中,二叉樹是每個節點最多有兩個子樹的樹結構。通常子樹被稱作左子樹(left subtree)和右子樹(right subtree)。二叉樹常被用于實現二叉查找樹和二叉堆。二叉樹的每個結點至多只有二棵子樹(不存在度大于2的結點)。二叉樹的子樹有左右之分,次序不能顛倒。二叉樹的第i層至多有2^{i-1}個結點;深度為k的二叉樹至多有2^k-1個結點;對任何一棵二叉樹T,如果其終端結點數為n_0,度為2的結點數為n_2,則n_0=n_2+1。一棵深度為k,且有2^k-1個節點稱之為滿二叉樹;深度為k,有n個節點的二叉樹,當且僅當其每一個節點都與深度為k的滿二叉樹中,序號為1至n的節點對應時,稱之為完全二叉樹。?哈希表:?散列表(Hash table,也叫哈希表),是根據關鍵碼值(Key value)而直接進行訪問的數據結構。也就是說,它通過把關鍵碼值映射到表中一個位置來訪問記錄,以加快查找的速度。這個映射函數叫做散列函數,存放記錄的數組叫做散列表。給定表M,存在函數f(key),對任意給定的關鍵字值key,代入函數后若能得到包含該關鍵字的記錄在表中的地址,則稱表M為哈希(Hash)表,函數f(key)為哈希(Hash) 函數。Map類中存儲的鍵—值對通過鍵來標識,所以鍵值不能重復。?Object put(Object key,Object value);       //放入一對東西?,返回原key的valueObject get(Object key);                              //通過key找到value?Object remove(Object key);?                      //去掉key對應的value,key隨之也去掉boolean containsKey(object key);?           //是不是包含keyboolean containsKey(object value);int size();                                                       多少對boolean isEmpty()void putAll?(Map t);                                       把另一個Map所有值加進來?void clear();??例:public class TestMap {    public static void main(String agrs[]) {        Map m1 = new HashMap();        Map m2 = new TreeMap();        m1.put("one", new Integer(1));        m1.put("two", new Integer(2));        m1.put("three", new Integer(3));        m2.put("A", new Integer(1));        m2.put("B", new Integer(2));        System.out.println(m1.size());        System.out.println(m1.containsKey("one"));        System.out.println(m2.containsValue(new Integer(1)));        if (m1.containsKey("two")) {            int i = ((Integer) m1.get("two")).intValue();//get two返回的是object,強制轉換成integer,然后獲得int的值            System.out.println(i);        }        Map m3 = new HashMap(m1);        m3.putAll(m2);        System.out.println(m3);    }}結果是:3truetrue2{A=1, B=2, two=2, three=3, one=1}?6.Auto-boxing/unboxing?在合適的時機自動打包,解包,自動將基礎類型轉換成對象,自動將對象轉換成基礎類型?public class TestMap {    public static void main(String agrs[]) {        Map m1 = new HashMap();        Map m2 = new TreeMap();        m1.put("one", 1);         //打包        m1.put("two", 2);        m1.put("three", 3);        m2.put("A", 1);        m2.put("B", 2);        System.out.println(m1.size());        System.out.println(m1.containsKey("one"));        System.out.println(m2.containsValue(1));        if (m1.containsKey("two")) {            int i = ((Integer) m1.get("two"));   //Integer是不能去掉的,對象轉換成Integer后,才能自動轉換成int類型            System.out.println(i);        }        Map m3 = new HashMap(m1);        m3.putAll(m2);        System.out.println(m3);    }}將TestMap加以修改,結果相同
 7.泛型?JDK1.4以前類型不明確:裝入集合的類型都被當成Object對待從而失去自己的實際類型。解決辦法:在定義集合的時候同時定義集合中對象的類型。?示例:BasicGeneric,可以在定義Collection時定義,也可以在循環時用Iterator指定?舉例:public class BasicGeneric {    public static void main(String agrs[]) {        List c = new ArrayList();        c.add("aaa");        c.add("bbb");        c.add("ccc");        for (int i = 0; i < c.size(); i++) {            String s = c.get(i).toString();            System.out.println(s);        }        Collection c2 = new HashSet();        c2.add("AAA");        c2.add("BBB");        c2.add("CCC");        for (Iterator it = c2.iterator(); it.hasNext();){            String s = it.next().toString();            System.out.println(s);        }    }}public class MyName implements Comparable<MyName>{//對MyName類用泛型實現comparable    int age;    public int compareTo(MyName nm) {        if(this.age > nm.age) return 1;        else if(this.age < nm.age) return -1;        else return 0;    }}結果是:aaa   bbb  ccc  AAA  CCC  BBB泛型和打包綜合利用:?public class TestMap {    public static void main(String agrs[]) {        Map m1 = new HashMap();        m1.put("one", 1);        m1.put("two", 2);        m1.put("three", 3);        System.out.println(m1.size());        System.out.println(m1.containsKey("one"));        if (m1.containsKey("two")) {            int i = (int) m1.get("two");            System.out.println(i);        }    }}結果是:3true2?另一個例子:?public class TestArgsWords {    private static final int ONE = 1;    public static void main(String args[]) {        Map m = new HashMap();        for (int i = 0; i < args.length; i++) {            if (!m.containsKey(args[i])) {                m.put(args[i], ONE);            } else {                int freq = (int) m.get(args[i]);                m.put(args[i], freq + 1);            }        }        System.out.println                (m.size() + " distinct words detected:");        System.out.println(m);    }}結果:0  distinct words detected:?{}
新聞熱點
疑難解答