可能大家都没有注意,Collection接口的父接口是java.lang.Iterable

在这个接口中定义的是iterator方法
并且这个接口中指出 “实现这个接口允许对象成为 "foreach" 语句的目标”
这就是说,所有的集合类都要实现iterator方法
都可以返回一个Iterator对象,完成迭代功能
下面写一个示例代码演示一下package iterator;

 

import java.util.Iterator;

 

/*

* Jimliu

*

* 这个例子为了试验java.lang.Iterable接口中提到的:

*   实现这个接口允许对象成为 "foreach" 语句的目标。

*

* 自己实现一个保存int数据的ArrayList,可以动态增长

* 扩容的算法是 size = size + size/2 + 1;

* 提供get/add/toString/size/iterator方法和两种构造方式

* 实现Iterable接口,允许对象成为 "foreach"语句的目标(见测试类print3方法)

* 实现一个成员内部类Iterator(迭代器),通过它操作外部类的属性,实现hasNext和next方法

*/

public class MyIntArrList implements Iterable

{

        private static final int DEFAULT_SIZE = 10; // 初始默认大小

 

        private int size; // 当前数组最大保存多少元素

 

        private int[] arr; // 保存int值的数组

 

        private int index; // 当前有效元素个数

 

        public int size()

        {

                return index;

        }

 

        public MyIntArrList()

        {

                size = DEFAULT_SIZE;

                arr = new int[size];

                index = 0;

        }

 

        public MyIntArrList(int size)

        {

                if (size <= 0)

                {

                        throw new IllegalArgumentException("size must be > 0");

                }

                this.size = size;

                arr = new int[this.size];

                index = 0;

        }

 

        public void add(int n)

        {

                if (index == size) // 查看是否满了

                {

                        size = size + size / 2 + 1; // 扩容

                        int[] tmp = new int[size];

                        System.arraycopy(arr, 0, tmp, 0, index); // 拷贝

                        arr = tmp;

                }

                arr[index] = n;

                index++;

        }

 

        public int get(int ind)

        {

                if (ind < 0 || ind >= index)

                {

                        throw new ArrayIndexOutOfBoundsException(ind);

                }

                return arr[ind];

        }

 

        public String toString()

        {

                StringBuffer sb = new StringBuffer("[");

                for (int i = 0; i < index; i++)

                {

                        sb.append(arr[i]);

                        if (i != index - 1)

                                sb.append(", ");

                }

                sb.append("]");

                return sb.toString();

        }

 

        public Iterator iterator()

        {

                return new MyIterator(); // 成员内部类MyIterator

        }

 

        /*

         *  私有这个类,外界不能主动创建这个类的对象

         *  只能通过外部类的iterator()方法返回一个迭代器对象

         *  使用者得到这个迭代器类的对象后,

         *  可以调用公开的hasNext和next方法进行迭代操作

         *  remove方法没有实现,抛出异常

         */

        private class MyIterator implements Iterator // 实现Iterator接口

        {

                private int cnt = 0; // 当前迭代的下标

 

                public boolean hasNext()

                {

                        return cnt != index; // 没有迭代到最后就返回true

                }

 

                public Object next() // 保存当前数据,对cnt++,然后返回保存好的数据

                {

                        int n = arr[cnt];

                        cnt++;

                        return n;

                }

 

                public void remove() // 不支持,抛出不支持操作异常

                {

                        throw new UnsupportedOperationException();

                }

        }

}

复制代码下面是这个类的测试类package iterator;

 

import java.util.Iterator;

 

public class Test

{

        public static void main(String[] args)

        {

                MyIntArrList ti = new MyIntArrList();

                for(int i=0; i<40; i++)

                {

                        ti.add(i);

                }

                System.out.println(ti.toString());

                print1(ti);

                print2(ti);

                print3(ti);

        }

        // for get

        public static void print1(MyIntArrList ti)

        {

                System.out.println("=== for get ===");

                for (int i=0; i<ti.size(); i++)

                {

                        System.out.print(ti.get(i)+" ");

                }

                System.out.println();

        }

        // iterator

        public static void print2(MyIntArrList ti)

        {

                System.out.println("=== iterator ===");

                Iterator it = ti.iterator();

                while(it.hasNext())

                {

                        Object o = it.next();

                        System.out.print(o + " ");

                }

                System.out.println();

        }

        // foreach

        public static void print3(MyIntArrList ti)

        {

                System.out.println("=== for each ===");

                for (Object o : ti)

                {

                        System.out.print(o + " ");

                }

                System.out.println();

        }

}