-2

why the first code report the desired result and the second not (Java beginner)

public class p1 {
    public static void main(String[] args) throws IOException{
        int[] a = new int[100];
        int i = 0 ;
        for(int element1 : a){
            element1 = i++;
            System.out.println(element1);
        }
    }
}

result 0,1,2,3,......,99 second version:-

public class p1 {
public static void main(String[] args) throws IOException{
    int[] a = new int[100];
    int i = 0 ;
    for(int element1 : a){
        element1 = i++;
    } 
    for(int element2 : a){
        System.out.println(element2);
    } 
}

} result 0,0,0,0,.....,0

  • Because in the first code you set a value to `element1` before printing it. In the second code you don't set any value to `element2`. Can you describe why you were expecting a different result? – David Aug 30 '18 at 14:51
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – luk2302 Aug 30 '18 at 14:52
  • 1
    because OP is expecting the array content to change which does not happen in either case. – luk2302 Aug 30 '18 at 14:53
  • 2
    Read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for tips about how to debug your code. These tips also help you inspect code to determine how it works. – Code-Apprentice Aug 30 '18 at 14:53
  • Because you're iterating throw the array of zeros in the second case. – muzychuk Aug 30 '18 at 14:54
  • 2
    In the first example, add `System.out.println(Arrays.toString(a))` after the for loop. You'll be surprised what it outputs. – Code-Apprentice Aug 30 '18 at 14:55
  • does that mean that the array is still zeros in both cases – Mahmoud Emad Aug 30 '18 at 17:06

3 Answers3

2

It's because the array is not being updated:

for(int element1 : a){
    element1 = i;
    a[i++] = element1; //change array element's value to see the updated value
} 
for(int element2 : a){
    System.out.println(element2);
} 

This is applicable to both code snippets (neither is updating the array).

Just a side note: A for-loop makes more sense in this case case (rather than the for-each).:

for(int i = 0; i < a.length; i++){
    a[i] = i; 
} 
ernest_k
  • 44,416
  • 5
  • 53
  • 99
1

Based on your question, it seems like you're assuming that changing element1 will modify the value in the array. That's actually not the case. What actually happens is that element1 contains the same value as the corresponding value in the array - in a sense, it's a "copy" of it. Thus, when you set element1, all you're doing is setting element1, not the corresponding value in the array.

0

You did not assign a number to the array. In the first example it does not matter, because you are printing element1, which you just assign to a certain number. But not in the second example.

Hulk
  • 6,399
  • 1
  • 30
  • 52
Jiri Pur
  • 1
  • 1
  • This looks to be a comment if I'm honest and in fact there is already a comment to this effect on the question. – Caribou Aug 30 '18 at 15:24