美文网首页
interface Iterator

interface Iterator

作者: aliusa | 来源:发表于2018-01-22 21:08 被阅读0次

定义:对一系列对象(如集合)的迭代器.

/**还有没有*/

1.public boolean hasNext();

/**返回下一个对象并推进迭代器

*如果后面没有元素了call next时会抛出异常

*@throws NoSuchElementException*/

2.public E next();

/**删除上一个next返回的元素

*每call一次next才能call一次remove

*如果集合被定义为不能删除将抛异常

*@throws UnsupportedOperationException

*如果在call next之前call 了remove

*如果在call next 之后call 了两次或则两次以上的remove

*将抛出异常

*@throws IllegalStateException */

3.public void remove();

通用的遍历集合的迭代器的方式是:

Iterator iterator = collectionObject.Iterator();

取值

while(iterator.hasNext){

Object element = iterator.next

};

删除

while(iterator.hasNext){

interator.next

iterator.remove

};

相关文章

网友评论

      本文标题:interface Iterator

      本文链接:https://www.haomeiwen.com/subject/qfmraxtx.html