A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yaodd321 中级黑马   /  2014-11-3 23:12  /  938 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 yaodd321 于 2014-11-4 10:26 编辑

foreach是for语句的特殊简化版本,在遍历数组、集合方面,为开发人员提供很大的遍历。
foreach的语句格式:
for(元素类型t 元素变量x:遍历对象){
     引用了x的java语句;
}
下面是例子
用于数组;
  1. package cn.itcat.damo1;

  2. /*
  3. * foreach语句用于数组
  4. */
  5. import java.util.Random;

  6. public class foreach {

  7.         public static void main(String[] args) {
  8.                 int arr[] = new int[10];
  9.                 // 创建Random对象
  10.                 Random rand = new Random();
  11.                 for (int i = 0; i < 10; i++) {
  12.                         // 生成100以内的随机数
  13.                         arr = rand.nextInt(100);
  14.                 }
  15.                 // 遍历数组
  16.                 for (int x : arr) {
  17.                         System.out.print(x + " ");
  18.                 }
  19.         }
  20. }
  21. 用于集合:
  22. package cn.itcat.damo1;

  23. import java.util.Collection;
  24. import java.util.Collections;
  25. import java.util.LinkedList;

  26. public class foreachDemo {

  27.         public static void main(String[] args) {
  28.                 //创建集合
  29.                 Collection<String> cs = new LinkedList<String>();
  30.                 //将String[]中的元素添加到cs集合中
  31.                 Collections.addAll(cs, "Take the long way home".split(" "));
  32.                 //遍历集合
  33.                 for(String s:cs){
  34.                         System.out.print(s+" ");
  35.                 }
  36.         }

  37. }
复制代码

而我们要探究为什么foreach可以有这种功能。在java SE5中,引入了一个新的Iterable接口,改接口包含一个能够产生Iterator的iterator()方法,并且Iterable接口被foreach用来在序列中移动。所以,只要你创建的类实现了Iterable接口,就可以将它用于foreach语句
  1. package cn.itcat.damo1;

  2. import java.util.Iterator;

  3. public class IterableClass implements Iterable<String> {
  4.         protected String[] words = ("I will override the method of iterator from the class of Iterable"
  5.                         .split(" "));

  6.         // 覆写iterator方法
  7.         @Override
  8.         public Iterator<String> iterator() {
  9.                 // 匿名内部类
  10.                 return new Iterator<String>() {
  11.                         private int idex = 0;

  12.                         @Override
  13.                         // 序列移动的条件
  14.                         public boolean hasNext() {

  15.                                 return idex < words.length;
  16.                         }

  17.                         @Override
  18.                         // 返回遍历到的对象
  19.                         public String next() {

  20.                                 return words[idex++];
  21.                         }

  22.                 };
  23.         }

  24.         public static void main(String[] args) {
  25.                 for (String s : new IterableClass()) {
  26.                         System.out.print(s + " ");
  27.                 }
  28.         }

  29. }
复制代码

评分

参与人数 1黑马币 +1 收起 理由
杨佳名 + 1

查看全部评分

3 个回复

倒序浏览
不错,看下并复习下
回复 使用道具 举报
nice学习了
回复 使用道具 举报
看了一下,学习了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马