特點:存儲對象;長度可變;存儲對象的類型可不同;
(add(index, element)、add(index, Collection)、remove(index)、 set(index,element)、get(index)、subList(from, to)、listIterator())
ArrayList:底層是數組結構,查詢快,增刪慢,不同步。LinkedList:底層是鏈表結構,增刪快,查詢慢,不同步 addFist();addLast() getFirst();getLast() removeFirst();removeLast() 獲取并刪除元素,無元素將拋異常:NoSuchElementException 替代的方法(JDK1.6): offerFirst();offerLast(); peekFirst();peekLast();無元素返回null pollFirst();pollLast();刪除并返回此元素,無元素返回null Vector:底層是數組結構,線程同步,被ArrayList取代了 注:了對于判斷是否存在,以及刪除等操作,以依賴的方法是元素的hashCode和equals方法 ArrayList判斷是否存在和刪除操作依賴的是equals方法第一種
Set<K> keySet()取出Map集合中的所有鍵放于Set集合中,然后再通過鍵取出對應的值
Set<String> keySet = map.keySet();Iterator<String> it = keySet.iterator();while(it.hasNext()){String key = it.next();String value = map.get(key);//….}第二種
Set<Map.Entry<K,V>> entrySet()取出Map集合中鍵值對的映射放于Set集合中,然后通過Map集合中的內部接口,然后通過其中的方法取出
Set<Map.Entry<String,String>> entrySet = map.entrySet();Iterator<Map.Entry<String,String>> it = entrySet.iterator();While(it.hasNext()){ Map.Entry<String,String> entry = it.next(); String key = entry.getKey(); String value = entry.getValue(); //…… }用于操作數組對象的工具類,全為靜態方法
asList():將數組轉為list集合 好處:可通過list集合的方法操作數組中的元素:isEmpty()、contains()、indexOf()、set()a. 弊端:數組長度固定,不可使用集合的增刪操作。 如果數組中存儲的是基本數據類型,asList會將數組整體作為一個元素存入集合 集合轉為數組:Collection.toArray(); b.好處:限定了對集合中的元素進行增刪操作,只需獲取元素
新聞熱點
疑難解答