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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 芦曦 中级黑马   /  2012-7-14 22:33  /  4357 人查看  /  14 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 芦曦 于 2012-7-16 17:00 编辑

class Xunhuan
{
        public void xunhuan(int arr[])
        {
                 for (int x : arr) //这里for的作用是什么???
                System.out.println(x);
        }
}
class  S1
{
        public static void main(String[] args)
        {
                 int arr[] = {1,2,3,4,5,7};
                 Xunhuan x = new Xunhuan();
                 x.xunhuan(arr);
                }
}

14 个回复

倒序浏览
本帖最后由 包晗 于 2012-7-14 22:43 编辑

这是 for each循环
for each是5.0新增加的一个循环结构
for (循环变量类型 循环变量名称 : 要被遍历的对象)
{
      循环体
}

  1. class Xunhuan
  2. {
  3. public void xunhuan(int arr[])
  4. {
  5. for (int x : arr)
  6. System.out.println(x);
  7. }
  8. }
  9. class S1
  10. {
  11. public static void main(String[] args)
  12. {
  13. int arr[] = {1,2,3,4,5,7};
  14. Xunhuan x = new Xunhuan();
  15. x.xunhuan(arr);
  16. }
  17. }
复制代码
这里FOR 的作用是输出 数组里的数
输出结果为 123457
这个挺好理解的
希望对LZ有帮助  
谢谢~

评分

参与人数 1黑马币 +30 收起 理由
刘笑 + 30 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 陈冲 于 2012-7-14 22:43 编辑
  1. class Xunhuan

  2. {

  3.         public void xunhuan(int arr[])

  4.         {

  5.                  for (int x : arr) //这里for的作用是什么???

  6.                 System.out.println(x);

  7.         }

  8. }

  9. class  S1

  10. {

  11.         public static void main(String[] args)

  12.         {

  13.                  int arr[] = {1,2,3,4,5,7};

  14.                  Xunhuan x = new Xunhuan();

  15.                  x.xunhuan(arr);

  16.                 }

  17. }
复制代码
for (int x : arr) ;
System.out.println(x);
这两个语句合起来相当于
for(int x=0;x<arr.length;x++)
System.out.println(arr[x]);
输出结果是
1
2
3
4
5
7


评分

参与人数 2技术分 +1 黑马币 +30 收起 理由
滔哥 + 1 神马都是浮云
刘笑 + 30 赞一个!

查看全部评分

回复 使用道具 举报
高级for循环,建议看张老师加强视频,讲解的非常透彻。
回复 使用道具 举报
class Xunhuan
{
        public void xunhuan(int arr[])
        {
                 for (int x : arr)
                System.out.println(x);
        }
}
class  S1
{
        public static void main(String[] args)
        {
                 int arr[] = {1,2,3,4,5,7};
                 Xunhuan x = new Xunhuan();
                 x.xunhuan(arr);
                }
}
这里for的作用是:遍历数组的的每个元素,每遍历一次,用变量X来接收,是For循环的增强。是Java.5.0的新特性,适合数组和集合遍历其中元素,不适合用于遍历基本数据。
格式为  for(元素类型 元素名:集合(或数组))
for (int x : arr)
   System.out.println(x); 与下面的for循环效果相同
for(int x = 0, x<arr.length, x++){
        System.out.println(x);
}

     public static void main(String[] args)
        {
                 int arr[] = {1,2,3,4,5,7};
                 Xunhuan x = new Xunhuan();
                 x.xunhuan(arr);
                }
可以直接写为:
   public static void main(String[] args)
        {
                 int arr[] = {1,2,3,4,5,7};
               for(int x : arr){
                       System.out.printlln(x);
                    }
          }


评分

参与人数 2技术分 +2 收起 理由
滔哥 + 1
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
class Xunhuan
{
        public void xunhuan(int arr[])
        {
                 for (int x : arr) // 高级for循环;格式:for(数据类型 变量名:被遍历的集合或数组){};
                                   //在对数组进行遍历时和普通for循环相同,但在遍历集合时,只能获得元素
                                      //而不能对集合进行操作;
                System.out.println(x); //这两个语句合起来相当于;;for(int x=0;x<arr.length;x++)
                                                                                              //      {System.out.println(arr[x]);}
      
                                                            
        }
}
class  S1
{
        public static void main(String[] args)
        {
                 int arr[] = {1,2,3,4,5,7};
                 Xunhuan x = new Xunhuan();
                 x.xunhuan(arr);
                }
}

//高级for循环其实就是for循环的简体版,只是写的简单些,但不如普通for可观性强。
//同样是遍历工具的迭代器,不仅能遍历,还可以对元素进行删除操作,如果是ListIterator,还可以
//进行增删改查等操作。



评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
黑马程序员_毕向东_Java基础视频教程第17天-18-集合(增强for循环)    里面讲到

这个是高级for循环
格式:
for(数据类型 变量名:被遍历的集合(Collection)或者数组)
{

}
优点是:这样写可以使代码变得简洁。
但是这用法具有局限性:
1:访问的元素不能被赋值
2:不可以同时遍历两个结构,比如两个数组
3:只适合单一元素读取
4:只能向前单个元素的迭代
5:只支持java5以后的
当然也二维数组中也有用武之地,具体你看毕老师这课吧,我不太记得了。
下面是我运行的结果

未命名.jpg (9.65 KB, 下载次数: 24)

未命名.jpg

评分

参与人数 1技术分 +1 收起 理由
黑马张扬 + 1

查看全部评分

回复 使用道具 举报
它的作用是把参数数组arr中的每一个元素赋值给变量x。

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

回复 使用道具 举报
格式:
for(数据类型 变量名:被遍历的集合(Collection)或者数组)
{
}
这个是高级for循环 。


在eclipse中调用时名称时foreach, 格式如下:
for (iterable_type iterable_element : iterable) {        
}
回复 使用道具 举报
循环读取集合或对象中的单个元素和C#当中的foreach是一样的

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 新手 奖励一分

查看全部评分

回复 使用道具 举报
JDK1.5加入的增强for和循环.

语法是:  for(ElementType element:arrayName){};
举个例子:

    int[] numArray = { 2, 1, 3, 6, 5, 8 };
    for (int i : numArray) {
      System.out.print(i);
    }
打印的输出结果为:213658

    int[] numArray = { 2, 1, 3, 6, 5, 8 };
    for (int i=0; i < numArray.length; i++) {
      System.out.print(numArray);
    }
和这个相结合的集合的概念泛型也是JDK1.5新加入的,可以做到简化代码的作用.比如

   List<String> stringList = new ArrayList<String>();
   for (String str : stringList ) {
      System.out.print(str);// 可以打印出stringList 中的所有元素
    }

增强for(variable:object){body};

Object中是一个数组对象,或者是带有泛性的集合.
variable定义了一个局部变量,这个局部变量的类型与part2中的对象元素的类型是一致的.
body当然还是循环体.

点评

这一看就是复制粘贴的,警告一次 下次清理技术分为零  发表于 2012-7-16 14:51
回复 使用道具 举报
这是JDK5.0 for的新用法,比如你这个例子for(int x:arr){···}表示的就是取arr数组里的每一分子给变量X,注意的是它们的类型必须相同,这样对于数组的访问就会快狠多,而且是从数组的第一个到最后一个,这也是for新特性的一个缺点
回复 使用道具 举报
本帖最后由 陈少文 于 2012-7-16 16:36 编辑

/*
for循环增强
  
  功能:遍历元素。
  java5.0新特性,特点:使代码更加简洁
*/

public class XunHuan
{
          public static void main(String[] args)
          {
                  int arr[] = {1,2,3,4,5,7};
   
                   //for循环加强方法
                  for(int x:arr) //相当于arr[x]   0<=x<arr.length,x++
                   System.out.println(x);
             }
          //常用的一般方法
            static void getFor(int arr[])
            {
                    for(int i = 0;i<arr.length;i++)
                     {
                               System.out.println(arr);
  }
              }
}

回复 使用道具 举报
你这里的for的作用:遍历数组的的每个元素,每遍历一次,用x来接收,是增强的for循环。

增强for循环和iterator遍历的效果是一样的。

但是增强for循环有些缺点,例如不能在增强循环里动态的删除集合内容。不能获取下标等。
回复 使用道具 举报
这是增强for循环

格式:(数据类型 变量名:被遍历的集合(Collection)或者数组)

for(String s :al)

{

       System.out.println(s);

}
传统for循环与高级for循环的区别:

高级for循环有一个局限性:必须有被遍历的目标。

在遍历数组时,还是使用传统for循环,因为传统for循环有角标。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马