Java基本問題(4)

來源:互聯網
上載者:User

標籤:system   基本   col   轉換   cas   map   sys   異常   wan   

/* * 未使用泛型面對的問題 * 1、可以添加任何類型元素 * 2、類型轉換出現ClassCastException異常 * * 在集合中使用泛型,規定添加元素的類型 */ @Test public void test01() { List l = new ArrayList(); l.add(123); l.add(222); l.add("abc"); l.add(new a("lisi", 23)); l.add(333);// java.lang.ClassCastException 異常// Collections.sort(l); List<Integer> l1 = new ArrayList<Integer>(); l1.add(123); l1.add(222);// l1.add("abc"); 不能添加// l1.add(new a("lisi", 23)); 不能添加 l1.add(333); System.out.println(l1); TreeMap<a, Integer> m = new TreeMap<a, Integer>(); m.put(new a("lisi",23), 10000); m.put(new a("zhangs",34), 20000); m.put(new a("lisi",55), 100000); m.put(new a("zhangliu",12), 2000); m.put(new a("wangw",45), 20000); Set<Map.Entry<a, Integer>> set = m.entrySet(); Iterator<Map.Entry<a, Integer>> i = set.iterator(); while (i.hasNext()) { System.out.println(i.next()); } }

Java基本問題(4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.