List的遍历

吃饭睡觉不准打豆豆 2019-04-17 10:02:24
有人能解释下向前迭代和向后迭代吗,我怎么看下面的迭代顺序都是向前,难道是这两者的区分不是按照方向来的吗


for (ListIterator<Type> it = list.listIterator(list.size()); it.hasPrevious(); ) {
Type t = it.previous();
...
}
...全文
65 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2019-04-17
  • 打赏
  • 举报
回复
引用 2 楼 江南一叶竹筏 的回复:
这是oracle 的官方教程展示的,按照他这个意思,下面的代码是向后迭代 Here's the standard idiom for iterating backward through a list. for (ListIterator<Type> it = list.listIterator(list.size()); it.hasPrevious(); ) { Type t = it.previous(); ... }
人家说的是用iterator backward,倒序遍历
  • 打赏
  • 举报
回复
有些明白了,可能这里的意思说的不是向前或者向后迭代,它要表达的是顺序迭代和倒序迭代,那上面的就是倒序迭代
  • 打赏
  • 举报
回复
这是oracle 的官方教程展示的,按照他这个意思,下面的代码是向后迭代 Here's the standard idiom for iterating backward through a list. for (ListIterator<Type> it = list.listIterator(list.size()); it.hasPrevious(); ) { Type t = it.previous(); ... }
qybao 2019-04-17
  • 打赏
  • 举报
回复
Type t = it.previous(); //这个是向前
Type t = it.next(); //这个是向后

同样的,用hashNext是判断后方是否还有元素

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧