package cn.itcast.jdk15;java
import java.util.Iterator;ide
//自定一個類使用加強for循環
class MyList implements Iterable<String>{
Object[] arr = new Object[10];
int index = 0 ; //當前的指針
public void add(Object o){
arr[index++] = o; // 1
}
public int size(){
return index;
}指針
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {rem
int cursor = 0;it
@Override
public boolean hasNext() {
return cursor<index;
}for循環
@Override
public String next() {
return (String) arr[cursor++];
}ast
@Override
public void remove() {
}
};
}
}class
public class Demo3 {
public static void main(String[] args) {
MyList list = new MyList();
list.add("張三");
list.add("李四");
list.add("王五");
for(String item :list){
System.out.println(item);
}
}
}import