int[] test=new int[10];
int count=0;
for(int i:test) i=count++;这里不能对i赋值??,出来的还是0,foreach是不是不能赋值呢
但是
ArrayList<Employee> test=new ArrayList<Employee>();
.....
...
for(Employee e:test) e.raisesalary(5);
这是书上的例子,这样可以foreach里引号前面定义的东西是什么呢,生存期限是多长?它的处理过程是怎么样的?

解决方案 »

  1.   

    foreach里引号前面定义的东西是什么呢,是 Employee 的对象
      

  2.   

    int[] test=new int[10]; 
    int count=0; 
    for(int i:test) i=count++; 你的test数组里面有东西吗? 我觉得显然没有 test里面没有东西自然不能赋值给int i任何返回一个数组的方法都可以用foreach  例如 
      

  3.   

    Try this:int[] test = new int[10];

    for (Integer i : test)
    {
    i = 10;
    System.out.println(i);
    }
      

  4.   


    public class ForEachInt {
      public static void main(String[] args) {
        for(int i : range(10)) // 0..9
          printnb(i + " ");
        print();
        for(int i : range(5, 10)) // 5..9
          printnb(i + " ");
        print();
        for(int i : range(5, 20, 3)) // 5..20 step 3
          printnb(i + " ");
        print();
      }
    } /* Output:
    0 1 2 3 4 5 6 7 8 9
    5 6 7 8 9
    5 8 11 14 17
    *///:~
      

  5.   

    就是每次把test中的一个数赋给i,然后对i做操作,比一般的for省了些东西,但是只有实现了迭代器接口的类才可以使用foreach循环,这点注意哦
      

  6.   

    for(int i:test)
    这里的i相当于
    for(int j=0;j<test.length;j++){
       int i=test[j];   // 这个i
       
    }
      

  7.   

    int[] test=new int[10];
    int count=0;
    for(int i:test) i=count++; This for loop is equivalent to:
    for (int j=0; j<test.length; j++)
    {
        int i = test[j];
        i = count++;
    }You can see that test[j] is not modified.
      

  8.   

    int[] test = new int[10];
            
    for (Integer i : test)
    {
        i = 10;
        System.out.println(i);
    }
    This is equivalent to:for (int j=0; j<test.length; j++)
    {
        Integer i = test[i]; //Autoboxing from int to Integer
        i = 10; //Autoboxing as well
    }
      

  9.   

    可以对i赋值,它的生命周期仅是在foreach中。
    其实它就是一个引用。遍历数组中的每一个元素。
      

  10.   


    原来是这样,不过既然有一个赋值过程,为什么不报错呢,我debug的时候好像它直接跳过去了
      

  11.   


    int[] test=new int[10]; 
     int count=0; 
     for(int i:test) i=count++; 
    这里是可以对i赋值的。
    引号前面定义的是保存数组中元素的临时变量,把数组中的元素的值一次赋给它。
    这和for(int i; i < N; i++)里面的i是两回事。 for(Employee e:test) e.raisesalary(5); 
     这是书上的例子,这样可以 
    这里数组test的类型是引用数据类型,所以取出里面的元素e也是,e.raisesalary(5);就是调用其中的方法,进行相应的操作。