2

Why does think not work? It just prints zeros. However it works when I use a normal for loop with an index value 'i' and using 'a[i]' inside the body of the loop.

The problem is not with the printing loop, as it does not print the values, even with a normal for loop.

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    int[] a = new int[5];
    for (int i : a)
    {
        System.out.println("Enter number : ");
        i=s.nextInt();

    }
    System.out.println("\nThe numbers you entered are : \n");
    for (int i : a)
    {
        System.out.println(i);
    }
}
}
Vikram
  • 227
  • 2
  • 5
  • 13

5 Answers5

7

When you access the element using enhanced for-loop: -

for (int i : a)
{
    System.out.println("Enter number : ");
    i=s.nextInt();

}

Here, int i is a copy of element in the array. When you modify it, the change will not get reflected in the array. That's why the array elements are 0.

So, you need to iterate using traditional for-loop and access the array elements on that index to assign values to it.

Even if your array was an array of some reference, it would still not work. That's because, the variable in a for-each is not a proxy for an array or Collection reference. For-each assigns each entry in the array to the variable in the loop.

So, your enhanced for-loop: -

for (Integer i: arr) {
    i = new Integer();
}

is converted to: -

for (int i = 0; i < arr.length; i++) {
    Integer i = arr[i];
    i = new Integer();
}

So, the initialization of i in the loop, is not reflected in the array. And thus the array elements are null.

Workarond: -

  1. use traditional for loop: -

    for (int i = 0; i < a.length; i++) {
        a[i] = sc.nextInt();
    }
    
Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
  • 1
    Ohh. Something like pass by value and not pass by reference. Thanks! – Vikram Nov 06 '12 at 13:50
  • 1
    @Vikram.. Well, everything in JAva are passed by value. Point is that, references are passed by value of reference. – Rohit Jain Nov 06 '12 at 13:50
  • The first workaround does not work (prints null). I assume that it's because Integer objects are immutable. So even though we are passing a reference to the Integer object, the statement "val=sc.nextInt();" allocates a new Integer object inside the for loop and assigns it to val, leaving the original object unchanged. – Vikram Nov 06 '12 at 14:02
  • @Vikram.. No `Integer` is not immutable objects. Actually you can't initialize an array using an `enhanced for-loop`. This is because, the variable in the for loop is a local copy and is not actually pointing to the array element. This will not work even for array of references. – Rohit Jain Nov 06 '12 at 14:10
  • What exactly do you mean by a "local copy"? Are you saying that even though we are copying the pointer-to-object to for loop, it still doesn – Vikram Nov 06 '12 at 14:25
  • @Vikram.. There is no concept of `pointer` in Java. Its called `Reference`. And yes it is exactly that. – Rohit Jain Nov 06 '12 at 14:27
  • @Vikram.. See this post: - http://stackoverflow.com/questions/720200/why-doesnt-this-for-each-loop-work for more detailed answer. – Rohit Jain Nov 06 '12 at 14:30
2

Your first for loop should be a "classical" for loop :

for (int i = 0; i < a.length; i++) {
    a[i] = s.nextInt();
}
Jerome
  • 1,977
  • 1
  • 15
  • 31
2

i=s.nextInt();

This is assigning to a local variable i, not the array element. Essentially,

 for (int i : a) {
   //  body
 }

is same as

for (int index = 0; index < a.length; index++) {
    int i = a[index];
    // body
}

So whatever you assign to i does not affect the array.

Miserable Variable
  • 27,314
  • 13
  • 69
  • 124
1

the statement

i=s.nextInt()

Ask yourself :- what are you doing here?

you are taking input from user. BUT where are you storing it? inside i? which will get overwritten with each iteration. Also i is local scoped to for loop. So it doesnt exists outside the containing loop.

So you will get 0... 0 ..0 as output.

The solution :

for (int i=0;i<a.length;i++) {
a[i] = s.nextInt();
}

store the values user input inside the array.

Mukul Goel
  • 8,317
  • 5
  • 34
  • 72
-2

You should store the values you get from s.nextInt();, this can be achieved by :

int j = 0;
for (int i : a){
      System.out.println("Enter number : ");
      a[j++]=s.nextInt();

}

This should work.

tmwanik
  • 1,565
  • 13
  • 19
  • 2
    And since you don't use `i` within the loop it would be nicer to use the traditional for-loop. – jlordo Nov 06 '12 at 14:08