首页 > 其他 > 详细

forEach与迭代器

时间:2017-10-27 14:09:45      阅读:246      评论:0      收藏:0      [点我收藏+]
任何实现了Iterable接口的类,都可以用forEach语句,并且Java中大部分的Collection都可以用forEach,除了Map
1
import java.util.*; 2 public class AdapterMethodiom implements Iterable<String> 3 { 4 protected String[] words = ("hello world this is a good day").split(" "); 5 public Iterator<String> iterator() { 6 return new Iterator<String>(){ 7 private int index =0; 8 9 @Override 10 public boolean hasNext() { 11 return index < words.length; 12 } 13 14 @Override 15 public String next() { 16 17 return words[index++]; 18 } 19 20 }; 21 22 } 23 public static void main(String[] args) { 24 for(String s:new AdapterMethodiom()) 25 { 26 System.out.println(s+" "); 27 } 28 29 } 30 }

 

forEach与迭代器

原文:http://www.cnblogs.com/vector11248/p/7742544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!