Java中Collecion、List总结及注意事项

总结
Collection
概念:
API:
增:
boolean add(E e)
boolean addAll(Collection c)//只要原集合发生修改,便放回true
删:
void clear()
boolean remove(Object o)
boolean removeAll(Collection c)
boolean retainAll(Collection c)
查:
boolean contains(Object o)
boolean containsAll(Collection c) //只要有交集,便返回true
获取集合的属性:
int size()
boolean isEmpty()
遍历:
Object[] toArray()
Iterator iterator()

Iterator
概念:
API:
boolean hasNext()
E next()
void remove()

设计原理:迭代器模式
注意事项:
a.如果通过集合的API修改了集合的结构,那么所有迭代器都应该失效。
b.如果通过某个迭代器修改了集合的结构,那么所有其它迭代器都应该失效。
c.使用迭代器遍历的时候,不应该使用while循环,可以使用for,最好使用foreach

List
概念:
API:
增:
void add(int index, E e)
boolean addAll(int index, Collection c)
删:
E remove(int index)
改:
E set(int index, E e)
查:
E get(int index)
int indexOf(Object o)
int lastIndexOf(Object o)
遍历:
ListIterator listIterator()
ListIterator listIterator(int index)

ListIterator支持向前遍历

int prevousIndex();

boolean hasPrevious(); 

int previousIndex();

E previous();

获取子串:
List subList(int fromIndex, int toIndex)

 

 it.Add( )和it.remove( )  连续使用会报错,Add()方法执行后,lastset=-1;

void add(E,e) // 做了两件事,在光标处增加元素,光标后移,

posted @ 2020-07-12 23:35  JC97  阅读(187)  评论(0编辑  收藏  举报