#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3

    Enhanced for loop


    I've been playing aroud a lil bit with that new enhanced for loop, and ran into some problem.

    consider this following code:

    Code:
    public class Test {
    
            public static void main(String[] args) {
    
                    int[] myArray=new int[10];
    
                    for (int i=0; i<10; i++) {
                            myArray[i]=(int)(Math.random()*11);
                    }
    
                    for (int i=0; i<10; i++) {
                            System.out.println(myArray[i]);
                    }
            }
    }
    this will compile, run and produce the correct output.

    but when I change it to this:

    Code:
    public class Test {
    
            public static void main(String[] args) {
    
                    int[] myArray=new int[10];
    
                    for (int i: myArray) {
                            i=(int)(Math.random()*11);
                    }
    
                    for (int i: myArray) {
                            System.out.println(i);
                    }
            }
    }
    it suddenly produces zero's:

    output:
    Code:
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    can someone please explain what is wrong with this code?
    thanks in advanced!
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    With the enhanced for loop, you aren't able to change the elements in the array. You can access them.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    Hi bullet, thanks for your help.

    Originally Posted by bullet
    With the enhanced for loop, you aren't able to change the elements in the array. You can access them.
    are you sure? because I also tried this:

    Code:
    public class Test {
    
            public static void main(String[] args) {
    
                    int[] myArray=new int[10];
    
                    for (int i: myArray) {
                            i=(int)(Math.random()*11);
                            System.out.println(i);
                    }
            }
    }
    and that produces a valid output (it prints 10 random numbers, which means the elements were changed).
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,959
    Rep Power
    4035
    Originally Posted by so.very.tired
    and that produces a valid output (it prints 10 random numbers, which means the elements were changed).
    No, the variable i changed, the original values of the myArray variable are not changed.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    Originally Posted by kicken
    No, the variable i changed, the original values of the myArray variable are not changed.
    I see...
    Ok, thanks for the help.

IMN logo majestic logo threadwatch logo seochat tools logo