1. 实现了Iterable接口类,必须实现方法:
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
2, 而实现了Iterator接口的类,必须实现方法:
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return null;
}
@Override
public T next() {
// TODO Auto-generated method stub
return null;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
通过观察发现,要是实现了Iterable接口,就要返回一个Iterator。所以可以看出来这两个接口的联系是十分紧密的。
实现了Iterable接口的类,就可以使用foreach的形式遍历该类的元素了。例如,如下两种形式的输出是一样的:
String[] arr="hello i am your god.".split(" ");
MyIterable myIterable=new MyIterable(arr);
//第一种
Iterator<String> iterator=myIterable.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("***************************************");
//第二种
for(String str : myIterable){
System.out.println(str);
}
到这里,姑且给两个例子看看:
第一个:
package com.wjy.iterable;
import java.util.Iterator;
import com.wjy.iterator.MyIterator;
public class MyIterable implements Iterable<String>{
private String[] arr;
public MyIterable(String[] arr){
this.arr=arr;
}
@Override
public Iterator<String> iterator() {
// TODO Auto-generated method stub
return new MyIterator(arr);
}
}
package com.wjy.iterator;
import java.util.Iterator;
public class MyIterator implements Iterator<String>{
private String[] arr;
private int index=-1;
private int size;
public MyIterator(String[] arr) {
// TODO Auto-generated constructor stub
this.arr=arr;
this.size=arr.length;
}
@Override
public boolean hasNext() {
boolean flag = index < size - 1;
if (index == size -1) {
index = -1;
}
return flag;
}
@Override
public String next() {
if (index < size - 1) {
index++;
return arr[index];
}
return null;
}
@Override
public void remove() {
// TODO Auto-generated method stub
for (int i = size - 1; i >= 0; i--) {
if (arr[i] != null) {
arr[i] = null;
return;
}
}
}
}
测试代码:
package com.wjy.test;
import java.util.Iterator;
import com.wjy.iterable.MyIterable;
public class MainTest {
public static void main(String args[]){
String[] arr="hello i am your god.".split(" ");
MyIterable myIterable=new MyIterable(arr);
Iterator<String> iterator=myIterable.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("***********************************************");
for(String str : myIterable){
System.out.println(str);
}
}
}
第二个:
package com.wjy.iterable;
import java.util.Iterator;
public class MyIterableNew implements Iterable<String>{
@Override
public Iterator<String> iterator() {
// TODO Auto-generated method stub
return new Iterator<String>() {
private String[] arr="ni hao wo shi shui ni zhi dao ma?".split(" ");
private int index=0;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return index<arr.length;
}
@Override
public String next() {
// TODO Auto-generated method stub
return arr[index++];
}
@Override
public void remove() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
};
}
}
测试代码:
package com.wjy.test;
import java.util.Iterator;
import com.wjy.iterable.MyIterableNew;
public class MyIterableNewTest {
public static void main(String args[]){
MyIterableNew myIterableNew=new MyIterableNew();
for(String string : myIterableNew){
System.out.println(string);
}
System.out.println("***********************************");
Iterator<String> iterator=myIterableNew.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
可以看出来,第二个例子采用了匿名类了。