Java编程思想 (第4版) 9.1分
读书笔记 第241页
Excelsior

public class IterableClass implements Iterable<String>{

protected String[] words=("And that is how we knwo the Earth to be banana-shaped."

.split(" "));

@Override

public Iterator<String> iterator() {

return new Iterator<String>(){

private int index=0;

public boolean hasNext() {

return index<words.length;

}

public String next() {return words[index++];}

public void remove() {throw new UnsupportedOperationException();} //Not implemented

};

}

public static void main(String[] args) {

for(String s:new IterableClass()) {

System.out.println(s+" ");

}

}

}

不存在任何从数组到Iterable的自动转换,你必须手动执行这种转换

0
《Java编程思想 (第4版)》的全部笔记 383篇
豆瓣
免费下载 iOS / Android 版客户端