java中迭代器Iterator,遍历集合删除集合中满足条件的元素

您所在的位置:网站首页 迭代结束的满足条件 java中迭代器Iterator,遍历集合删除集合中满足条件的元素

java中迭代器Iterator,遍历集合删除集合中满足条件的元素

2024-03-06 01:06| 来源: 网络整理| 查看: 265

迭代器(Iterator)   迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构。迭代器通常被称为“轻量级”对象,因为创建它的代价小。Java中的Iterator功能比较简单,并且只能单向移动:   使用方法iterator()要求容器返回一个Iterator。第一次调用Iterator的next()方法时,它返回序列的第一个元素。注意:iterator()方法是java.lang.Iterable接口,被Collection继承。   Iterator中有三个方法:分别为hasNext(),next(),remove()   (1) hasNext()方法

/** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #next} would * return an element rather than throwing an exception.) * * @return {@code true} if the iteration has more elements */ boolean hasNext();

从字面意思上看就是iteration中是否还有更多的元素   (2) next()方法

/** * Returns the next element in the iteration. * * @return the next element in the iteration * @throws NoSuchElementException if the


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3