aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Enhanced For Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Enhanced For Loop" Watch "Question on Enhanced For Loop" New topic
Author

Question on Enhanced For Loop

Krishna doddi
Greenhorn

Joined: Oct 09, 2006
Posts: 14
Can somebody explain me with an example how Enhanced For loop works. I tought I understood it well until I came accross this question


Ans : 0300

I tought there be a Runtime ArrayIndexOutOfBound Exception

Thanks
Siddharth


[HENRY: Added code tags]
[ October 26, 2006: Message edited by: Henry Wong ]

Sid<br />SCJP 1.5<br />SCBCD (Preparing....)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

The enhanced for loop will go through the array an set i to each member of the array.

1. On the first iteration, i will be 1, and set arr[1] to 0.
2. On the second iteration, i will be 0 (see step 1), and set arr[0] to 0.
3. On the third iteration, i will be 3, and set arr[3] to 0.
4. On the last iteration, i will be 0 (see step 3), and set arr[0] to 0.

So... final result should be 0030.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Krishna doddi
Greenhorn

Joined: Oct 09, 2006
Posts: 14
Thanks Henry,

Your explanation and a Pen and a Paper helped me.

Thanks,
Siddharth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Enhanced For Loop