• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Enhanced For Loop Problem

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have a prog
class ArrTest
{
public static void main(String[] args)
{
int []arr = {1,2,3,4};

for(int i=0;i<arr.length;i++){// line 1
System.out.println(i);
arr[i]=0;
}

for(int j : arr)

System.out.println(j);
}
}

The output is
0
1
2
3
0
0
0
0
Where as if i use enhanced for loop in place of line 1
the out put is
1
0
3
0
0
0
3
0

I always thought they both worked same way
can any body explain the different usage between these two for loops

Thanks
Charandeep
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------------------------------------------------------------------
for(int i=0;i<arr.length;i++){// line 1
System.out.println(i);//line 2
arr[i]=0;//line 3
--------------------------------------------------------------------

At line 2 you are printing the value of varibale i

and at line 3 you are assigning the 0 to every element in the array


if you replace the line 1 like this
for(int j:arr)
System.out.println(j);

you will get

1
2
3
4


But if you place the enhanced for loop after line 3
you will get alll 0's.Because you assigned the array elements to 0.



Thanks
Anil Kumar
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by this statement?

Where as if i use enhanced for loop in place of line 1
the out put is



Do you mean this?



If you did this, then it translates back to...



Remember, with the enhanced for loop. The loop variable is not the index, it is the element of the array.

Henry
 
Charandeep Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also always felt that when we use the enhanced for loop
it translates back to orignal for loop only
but

class ArrTest
{
public static void main(String[] args)
{
int []arr = {1,2,3,4};

for(int i: arr){
System.out.println("x" +i);
arr[i]=0;
}

for(int j : arr)

System.out.println("Hello World! x" +j);


}
}

The output is
x 1
x 0
x 3
x 0
Hello World! x 0
Hello World! x 0
Hello World! x 3
Hello World! x 0

But now if you change the program some what like this
class ArrTest
{
public static void main(String[] args)
{
int []arr = {1,2,3,4};

for(int i=0;i<arr.length;i++){
System.out.println("x" +i);
arr[i]=0;
}

for(int j : arr)

System.out.println("Hello World! x" +j);

}
}

The output is
x 0
x 1
x 2
x 3
Hello World! x 0
Hello World! x 0
Hello World! x 0
Hello World! x 0

This is the difference I am talkin about
Can you please explain why and wha is the difference between the two for loops.

Thanks
Charandeep
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is the difference I am talkin about
Can you please explain why and wha is the difference between the two for loops.



Charandeep,

Please go back an re-read my response. Basically, the variable "i" in the enhanced for loop is *not* the index, but the value.

This...



And this...



Are not the same logic...

Henry
[ May 12, 2007: Message edited by: Henry Wong ]
 
Charandeep Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry
Thanks for the explanation
I got your point
My question is
when the enhanced for loop is used on line 1
and after that arr[i]
that means value of i will be 1,2,3,4
so when it reaches 4 it should give ArrayIndexOutOfBoundException
which it doesn't
where as if you remove arr[i]=0;
It does give that
Can you clarify it further

Thanks
Charan
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If you do the following


int arr[] = {1,2,3,4};
for(int i : arr){
// line 1
System.out.println(i);
System.out.println("arr=" + arr[i]);
arr[i]=0;
}


On first iteration
i = 1;
a[i] = 0 => a[1] = 0 => arr = {1,0,3,4}

On second iteration
i = 0;
a[i] = 0 => a[0] = 0 => arr = {0,0,3,4}

On third iteration
i = 3
a[i] = 0 => a[3] = 0 => arr = {0,0,3,0} //this is the step tht prevents the OutOfBoundsexcetion

On forth iteration
i = 0
a[i] = 0 => a[4] = 0 => arr = {0,0,3,0}


This was tricky...I hope the above helps...

Thanks,
Megha
 
Charandeep Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot megha
Got it
Logical mistake from my end
thanks a lot for clarifying it
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic