Thinking 容器

P217泛型和類型安全的容器

不顯示指定泛型的將自動繼承自O(shè)bject,此時容器可裝任何類型的類,取出時需強(qiáng)制轉(zhuǎn)換為指定的類型。

public class ApplesAndOrangesWithoutGenerics {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < 3; i++)
            arrayList.add(new Apple());
        
        arrayList.add(new Orage());
        
        for (int i=0;i<arrayList.size();i++)
            System.out.println(((Apple)arrayList.get(i)).getId());  //最后一個取值報錯
    }
}

class Apple {
    private static long counter;
    private final long id = counter++;

    public long getId() {
        return id;
    }
}

class Orage {

}

結(jié)果

0
1
2
Exception in thread "main" java.lang.ClassCastException: eleven.Orage cannot be cast to eleven.Apple at eleven.ApplesAndOrangesWithoutGenerics.main(ApplesAndOrangesWithoutGenerics.java:15)

P220添加一組元素

/**
 * Arrays.asList():接收一個數(shù)組或者是用逗號分隔的元素列表
 * Collections.addAll():接收一個Collection對象、一個數(shù)組、用都逗號分隔的列表 傳統(tǒng)的addAll()方法
 */
public class AboutAsListAddAll {
    public static void main(String[] args) {
        Integer[] ints = { 1, 2, 3, 4, 5 };
        Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(ints));
        
        // 只能接收另一個Collection對象作為參數(shù),不靈活
        collection.addAll(Arrays.asList(6, 7, 8, 9, 10));   

        Collections.addAll(collection, 11, 12, 13, 14, 15); // 這個快,首選
        Collections.addAll(collection, ints);

        print(collection);

        List<Integer> list = Arrays.asList(ints);
        list.set(0, 0); //將數(shù)組第一個元素設(shè)置成0,能改變ints的值
        print(list);
        //會有運(yùn)行時錯誤,底層表示為數(shù)組,不能調(diào)整尺寸
//      list.add(1);
        
        List<Integer> list2 = new ArrayList<Integer>();
        list2.addAll(Arrays.asList(ints));
        list2.add(6);
        print(list2);
    }
    public static void print(Collection<Integer> c) {
        for (Integer i : c) System.out.print(i + " ");
        System.out.println();
    }
}

結(jié)果

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5
0 2 3 4 5
0 2 3 4 5 6

P222

容器的區(qū)別

  • List:
    以特定的順序保存元素

    • ArrayList:
      取快,存慢
    • LinkedList:
      取慢,存快
  • Set:
    元素不能重復(fù)

    • HashSet:
      最快的取元素方式,順序無意義
    • TreeSet:
      按照升序保存對象
    • LinkedHashSet:
      添加順序
  • Queue:
    在一端進(jìn),并從另一端出。

  • Map:
    關(guān)系數(shù)組,保存鍵值對

    • HashMap
    • TreeMap
    • LinkedHashMap
      按照添加順序保存鍵值,還保留了HashMap的速度

P234 Map統(tǒng)計隨機(jī)數(shù)生成的分布

public class Statistics {
    public static void main(String[] args) {
        Random rand = new Random(47);
        Map<Integer, Integer> m = new TreeMap<Integer, Integer>();
        for (int i = 0; i < 1000; i++) {
            int r = rand.nextInt(20);
            Integer freq = m.get(r);
            m.put(r, freq == null ? 1 : freq + 1);
        }
        System.out.println(m);
    }
}

結(jié)果

{0=42, 1=44, 2=53, 3=43, 4=44, 5=53, 6=42, 7=53, 8=46, 9=56, 10=58, 11=55, 12=48, 13=55, 14=52, 15=50, 16=53, 17=50, 18=51, 19=52}



















































這是空白頁
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,095評論 1 92
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 從三月份找實(shí)習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時芥藍(lán)閱讀 42,761評論 11 349
  • 陳王可閱讀 156評論 0 1
  • 這是心靈自由30天寫作群第三期第五篇作業(yè)。 今天作業(yè)的建議主題是:過往歲月中美好的人和事 今天是春節(jié)放假結(jié)束后的第...
    守望智慧閱讀 501評論 0 4

友情鏈接更多精彩內(nèi)容