The difference between collections and arrays
- Length of the array is fixed, the set of variable length
- Arrays can store basic data types, data types may be stored reference. Collection can only store reference data types
- Array can store the same type of data collection can store a variety of data types.
Common data structure
Stacks, queues, linked lists, arrays, trees, hash tables.
ArrayList, Vector, LinkedList three characteristics?
- ArrayList is an array structure of the underlying query fast, slow additions and deletions, thread-safe, high efficiency.
- LinkedList is a linked list data structure underlying, slow queries, additions and deletions fast, thread-safe, high efficiency.
- Vector is an array structure of the underlying query fast, slow additions and deletions, thread-safe, low efficiency.
Common methods for the collection
public boolean add(E e)
//添加一个元素
public boolean addAll(Collection c)
//添加一个集合的元素
public void clear()
//移除所有元素
public boolean remove(Object o)
//移除一个元素,返回的是是否删除成功
public boolean removeAll(Collection c)
//A集合.removeAll(B集合)取A集合中会踢除两个集合的交集元素之后的元素,移除之后A集合发生变化返回true否则返回flase
public boolean contains(Object o)
//判断集合中是否包含指定的元素
public boolean containsAll(Collection c)
//A集合.containsAll(B集合)A集合包含B集合中的所有元素返回true否则返回false
public boolean isEmpty()
//判断是否为空
public Iterator iterator()
//迭代器遍历集合
public boolean retainAll(Collection<?> c)
//A集合.retainAll(B集合)取A集合中取和A与B的交集,A集合发生变化返回true否则返回flase
public int size()
//集合的长度
public Object[] toArray()
//集合转为数组
- Some methods are more commonly used method set is the set of all common
list
public void add(int index, Object element)
// 在列表中的指定位置插入指定元素。
public Object remove(int index)
//根据对象删除元素。
public Object set(int index, Object element)
//替换指定索引处的元素,返回的是被替换的元素。
public Object get(int index)
//返回列表中指定位置的元素。
public ListIterator listIterator()
//列表迭代器(List集合特有的迭代器)。
ArrayList
public ArrayList()
//构造一个初始容量为10的空列表。
public ArrayList(int initCapcity)
//构造一个具有指定初始容量的空列表。
int indexOf(Object o)
//返回此列表中首次出现的指定元素的索引,或如果此列表中不包含指定元素,则返回-1。
int lastIndexOf(Object o)
//返回此列表中最后一次出现的指定元素的索引,或如果此列表中不包含指定元素,则返回-1。
Vector
public void addElement(E obj)
//将指定组件添加到此向量的末尾,将其大小增加1.
public E elementAt(int index)
//返回指定索引处的组件
public boolean removeElement(Object obj)
//从此向量中移除变量的第一个(索引最小的)匹配项。
public void removeElementAt(int index)
//删除指定索引处的组件
public void setElementAt(E obj, int index)
//将此向量指定 index 处的组件设置为指定的对象。
public Enumeration elements()
//返回此向量的组件的枚举,获取一个迭代器Enumeration
public boolean hasMoreElements()
//测试此枚举是否包含更多的元素。
public Object nextElement()
//如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。
LinkedList
public void addFirst(E e)
//将指定元素插入此列表的开头
public void addLast(E e)
//将指定元素插入此列表的结尾
public E getFirst()
//返回此列表的第一个元素
public E getLast()
//返回此列表的最后一个元素
public E removeFirst()
//移除并返回第一个元素
public E removeLast()
//移除并返回最后一个元素