Java集合类集的遍历方法-Iterator迭代器

Iterator又名迭代器,可以遍历集合对象的元素,主要接口和用法如下:

public interface Iterator;// 迭代器接口,一组指针序列
An iterator over a collection. Iterator takes the place of Enumeration in the collections framework. Iterators differ from enumerations in two ways:
    * Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
    * Method names have been improved.
// 抽象方法集:
1. boolean         hasNext()        // 还有下一个元素
          Returns true if the iteration has more elements.
2. E         next()        // 返回下一个元素
          Returns the next element in the iteration.
3. void         remove()        // 删除返回的最后一个元素
          Removes from the underlying collection the last element returned by the iterator (optional operation).
// 可迭代条件:实现Iterable接口;
public interface Iterable
Implementing this interface allows an object to be the target of the “foreach” statement.
构造迭代器:
Iterator         iterator()
          Returns an iterator over a set of elements of type T.   
所有Collection类都实现了该接口!
// 集合的输出:
1.Iterator,迭代输出,最标准的单向输出方式:从0–>未;
2.ListIterator,Iterator的字接口,针对List输出;
3.Enumeration,旧的接口,类似与Iterator;
4.foreach:输出数组或集合;
*一般不要删除Iterator指向的集合元素,一旦集合的结构变了,则当前指向他的Iterator也就失效了!
好比C++中的指针,一旦指向的数据没了,很可能造成空指针
——使用Iterator输出List:
package com.mldn;
import .util.List;
import .util.ArrayList;
import .util.Iterator;
public class IteratorList
{
        public static void main(String[] args)
        {
                List ls = new ArrayList();        // 实例化空List
                ls.add(“Hello”);
                ls.add(“, “);
                ls.add(“World!”);
                Iterator iter  = ls.iterator();                // 返回基于当前集合对象的迭代器实例
                while (iter.hasNext())        // 判断当前迭代器的指针是否指向空
                {
                        System.out.print(iter.next());        // 输出
                }
                System.out.println();       
        }
}
/*
administrator@xu-desktop:~$ com.mldn.IteratorList
Hello, World!
*/
———Iterator删除List成员:
package com.mldn;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorList
{
        public static void main(String[] args)
        {
                List ls = new ArrayList();        // 实例化空List
                ls.add(“Hello”);
                ls.add(“,”);
                ls.add(“World!”);
                Iterator iter  = ls.iterator();                // 返回基于当前集合对象的迭代器实例
                while (iter.hasNext())        // 判断当前迭代器的指针是否指向空
                {
                        String str = iter.next();
                        if (“,”.equals(str))        // 删除”,”
                        {
                                iter.remove();        // 删除本次返回的最后一个元素,基于Iterator的操作必须保证当前操作对象的一系列操作是由Iterator完成,
                        }                                        // 若此时用List内置的remove方法删除,则Iterator将失效,所有涉及Iterator的操作会瘫痪!
                        else
                        {
                                System.out.print(str);        // 输出
                        }
                }
                System.out.println();       
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.IteratorList
HelloWorld!
*/

———ListIterator:双向输出线性集合:
// 抽象方法集
boolean         hasNext()
          Returns true if this list iterator has more elements when traversing the list in the forward direction.
boolean         hasPrevious()
          Returns true if this list iterator has more elements when traversing the list in the reverse direction.
E         next()
          Returns the next element in the list.
int         nextIndex()
          Returns the index of the element that would be returned by a subsequent call to next.
E         previous()
          Returns the previous element in the list.
int         previousIndex()
          Returns the index of the element that would be returned by a subsequent call to previous.
void         add(E e)
          Inserts the specified element into the list (optional operation).         
void         remove()
          Removes from the list the last element that was returned by next or previous (optional operation).
void         set(E e)
          Replaces the last element returned by next or previous with the specified element (optional operation).
// 迭代器的声明必须指定泛型类型!
——用ListIterator双向遍历List:
package com.mldn;
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
public class DDOutput
{
        public static void main(String[] args)
        {
                List listSet =        new ArrayList();        // 实例化顺序集合
                listSet.add(“a”);
                listSet.add(“b”);
                listSet.add(“c”);
                listSet.add(“d”);
                listSet.add(“e”);
                ListIterator listIter = listSet.listIterator();        // 返回一个线性集合迭代器
                System.out.print(“由前到后输出:”);
                while (listIter.hasNext())        // 往下遍历
                {
                        System.out.print(listIter.next() + ” “);       
                }
                System.out.println();
                System.out.print(“由后到前输出:”);
                while (listIter.hasPrevious())        // 往上遍历,建立在往下遍历的基础上,必须先有前者才能遍历!
                {
                        System.out.print(listIter.previous() + ” “);
                }
                System.out.println();        // 换行
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.DDOutput
由前到后输出:a b c d e
由后到前输出:e d c b a
*/
———用ListIterator对List修改、添加内容:
package com.mldn;
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
public class DDOutput
{
        public static void main(String[] args)
        {
                List listSet =        new ArrayList();        // 实例化顺序集合
                listSet.add(“a”);
                listSet.add(“b”);
                listSet.add(“c”);
                listSet.add(“d”);
                listSet.add(“e”);
                ListIterator listIter = listSet.listIterator();        // 返回一个线性集合迭代器
                System.out.print(“由前到后输出:”);
                while (listIter.hasNext())        // 往下遍历
                {
                        String str = listIter.next();
                        System.out.print(str + ” “);       
                        listIter.set(“A” + str);        // 替换内容
                }
                System.out.println();
                System.out.print(“由后到前输出:”);
                listIter.add(“Over”);        // 往List中追加内容
                while (listIter.hasPrevious())        // 往上遍历
                {                       
                        System.out.print(listIter.previous() + ” “);
                }
                System.out.println();        // 换行
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.DDOutput
由前到后输出:a b c d e
由后到前输出:Over Ae Ad Ac Ab Aa
*/

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《Java集合类集的遍历方法-Iterator迭代器
本文地址:https://www.zhiletu.com/archives-118.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微