Re: ArrayList 的用法怎麼跟其他物件沒有一致性呢
※ 引述《dosiris (dosiris)》之銘言:
: 發現 ArrayList 的物件用法很特別
: 還要 ArrayList<String> xxx 才能建構帶有 String 的 ArrayList
: 不像一般 Array 的建構 只要 string[] xxx 就可以
: 但這不就代表語法上不一致嗎? 這樣並不是很漂亮的感覺
: 純粹個人看法 (搞不懂前者的使用為什麼要這樣獨樹一格...)
http://java.sun.com/docs/books/tutorial/collections/index.html
沒有必要一致,ArrayList屬於Collections中的成員
和Array是不同的東西,也複雜得多
Collections 分為Set, List, Queue, Map幾個基本的interfaces
有不同的功能,但都可以使用Collections中的method(ex: sort,shuffle,binarysearch)
其中List又分為 ArrayList, LinkedList
雖然操作介面一致但實作方式不同
ArrayList在random access比LinkedList快... O(1):O(n)
而LinkedList在insert element就比ArrayList快... O(1):O(n)
總之Array的結構簡單,但是要做一些處理時可能要自己寫code
Collections結構複雜(其實一般user碰不到,要用還蠻簡單的),
但有一致的操作介面以及一些預設的功能。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.111.79.32
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章