1.java中的集合學習
2.Collection中常用方法
1.添加功能
boolean add(E e):添加一個元素
boolean addAll(Collection<? extends E> c):添加一個集合
2.刪除功能
void clear():移除所有元素
boolean remove(Object o):移除一個指定元素
boolean removeAll(Collection<?> c):移除指定集合中的元素【只要有元素被移除,就返回true】
3.判斷功能
boolean contains(Object o):判斷集合中是否包含指定元素
boolean containsAll(Collection<?> c):判斷集合中是否包含指定集合元素【只有全部包含,才返回true】
boolean isEmpty():判斷集合是否為空
boolean equals(Object o)
4.獲取功能
Iterator<E> iterator():【父類中繼承的方法】
int hashCode():返回集合的哈希值
5.長度功能
int size():獲取集合中元素的個數
6.交集功能
boolean retainAll(Collection<?> c):兩個集合的交集【A對B做交集,結果保存在A中;只有A不變,才返回true】
7.把集合轉換成數組
Object[] toArray():把集合轉換成數組,可實現集合的遍歷
<T> T[] toArray(T[] a):
3.List特有的方法
1.添加功能
void add(int index, E element):在指定位置添加元素
boolean addAll(int index, Collection<? extends E> c):在指定位置添加集合
2.獲取功能
E get(int index):獲取指定位置的元素【與size()結合可實現遍歷】
List<E> subList(int fromIndex, int toIndex)
int indexOf(Object o)
int lastIndexOf(Object o)
3.列表迭代器
ListIterator<E> listIterator():List集合特有的迭代器
ListIterator<E> listIterator(int index)
4.刪除功能
E remove(int index):根據索引刪除元素,返回被刪除的元素
5.修改功能
E set(int index, E element):根據索引修改元素,返回被修改的元素
4.Vector特有的功能
1.添加功能
public void addElement(E obj)
public void insertElementAt(E obj, int index)
2.獲取功能
public E elementAt(int index)
public Enumeration<E> elements()
public int capacity():獲取集合的容量
public int indexOf(Object o, int index)
public int lastIndexOf(Object o, int index)
public E firstElement():獲取集合第一個元素
public E lastElement():獲取集合最后一個元素
3.復制功能
public void copyInto(Object[] anArray)
4.修改功能
public void ensureCapacity(int minCapacity):增加集合的容量
public void setElementAt(E obj, int index)
public void trimToSize():使集合等于當前大小
public void setSize(int newSize):設置集合的大小
5.移除功能
public void removeAllElements():移除所有元素,并大小設置為零
public boolean removeElement(Object obj):移除第一個元素
public void removeElementAt(int index):移除指定元素
PRotected void removeRange(int fromIndex, int toIndex):移除指定范圍元素【包括fromindex,不包括toIndex】
6.格式化功能
public String toString():返回集合的字符串形式【看源碼,重寫了】
LinkedList特有的方法:(可實現自定義棧)
public class LinkedList<E>extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, Serializable
1.添加功能
public void addFirst(E e)
public void addLast(E e)
2.獲取功能 3.public E getLast()
4public E getLast()
3.刪除功能
public E removeFirst()
public E removeLast()
新聞熱點
疑難解答