首页 > 编程语言 > 详细

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

时间:2020-07-13 00:40:29      阅读:82      评论:0      收藏:0      [点我收藏+]

总结
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) // 做了两件事,在光标处增加元素,光标后移,

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

原文:https://www.cnblogs.com/debug-the-heart/p/13290717.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!