iterator,hasNext,next
Next Iterator
2023-09-11 14:17:22 时间
JSONArray jsonArray = JSON.parseArray(orderInfoDto.getOrderDetail()); if (jsonArray != null && !jsonArray.isEmpty()) { // Iterator<Object> iterator = jsonArray.iterator(); // System.out.println(iterator); // iterator配合next Iterator it = jsonArray.iterator(); while (it.hasNext()) { JSONObject jsonObject = (JSONObject) it.next(); Integer shopId = (Integer) jsonObject.get("shopId"); } }
迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构。迭代器通常被称为“轻量级”对象,因为创建它的代价小。
Java中的Iterator功能比较简单,并且只能单向移动:
(1) 使用方法iterator()要求容器返回一个Iterator。第一次调用Iterator的next()方法时,它返回序列的第一个元素。注意:iterator()方法是java.lang.Iterable接口,被Collection继承。
(2) 使用next()获得序列中的下一个元素。
(3) 使用hasNext()检查序列中是否还有元素。
(4) 使用remove()将迭代器新返回的元素删除。
Iterator是Java迭代器最简单的实现,为List设计的ListIterator具有更多的功能,它可以从两个方向遍历List,也可以从List中插入和删除元素。
相关文章
- [Next.js] Override the Default Next.js Document
- [ES6] 14. Generator -- 1. yield & next()
- PHPNG (next generation)
- [Next.js] Override the Default Next.js Document
- [Next.js] Serve Optimized Images Using the Next.js Image Component
- [Next.js] Add Middleware to an API Route Created with next-connect
- [React] Create & Deploy a Universal React App using Zeit Next
- [ES6] 14. Generator -- 1. yield & next()
- [转]Nginx 502 PHP LNMP 502 终极解决方案 完美解决502 用 upstream 和 fastcgi_next_upstream 可以极大缓解
- python函数回顾:next()
- Hexo之Next主题美化代码