﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-twzheng's cppblog-文章分类-Java</title><link>http://www.cppblog.com/twzheng/category/21224.html</link><description>『站在风口浪尖紧握住鼠标旋转！』</description><language>zh-cn</language><lastBuildDate>Mon, 13 Jun 2016 15:47:22 GMT</lastBuildDate><pubDate>Mon, 13 Jun 2016 15:47:22 GMT</pubDate><ttl>60</ttl><item><title>Java学习之Iterator(迭代器)的一般用法</title><link>http://www.cppblog.com/twzheng/articles/213704.html</link><dc:creator>谭文政</dc:creator><author>谭文政</author><pubDate>Mon, 13 Jun 2016 15:23:00 GMT</pubDate><guid>http://www.cppblog.com/twzheng/articles/213704.html</guid><wfw:comment>http://www.cppblog.com/twzheng/comments/213704.html</wfw:comment><comments>http://www.cppblog.com/twzheng/articles/213704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/twzheng/comments/commentRss/213704.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/twzheng/services/trackbacks/213704.html</trackback:ping><description><![CDATA[迭代器（Iterator）

　　迭代器是一种设计模式，它是一个对象，它可以遍历并选择序列中的对象，而开发人员不需要了解该序列的底层结构。迭代器通常被称为&#8220;轻量级&#8221;对象，因为创建它的代价小。

　　Java中的Iterator功能比较简单，并且只能单向移动：

　　(1) 使用方法iterator()要求容器返回一个Iterator。第一次调用Iterator的next()方法时，它返回序列的第一个元素。注意：iterator()方法是java.lang.Iterable接口,被Collection继承。

　　(2) 使用next()获得序列中的下一个元素。

　　(3) 使用hasNext()检查序列中是否还有元素。

　　(4) 使用remove()将迭代器新返回的元素删除。

　　Iterator是Java迭代器最简单的实现，为List设计的ListIterator具有更多的功能，它可以从两个方向遍历List，也可以从List中插入和删除元素。

迭代器应用：
 list l = new ArrayList();
  l.add("aa");
  l.add("bb");
  l.add("cc");
  for (Iterator iter = l.iterator(); iter.hasNext();) {
   String str = (String)iter.next();
   System.out.println(str);
  }
  /*迭代器用于while循环
 Iterator iter = l.iterator();
  while(iter.hasNext()){
   String str = (String) iter.next();
   System.out.println(str);
  }
  */<img src ="http://www.cppblog.com/twzheng/aggbug/213704.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/twzheng/" target="_blank">谭文政</a> 2016-06-13 23:23 <a href="http://www.cppblog.com/twzheng/articles/213704.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>