File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enhanced For Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enhanced For Loop" Watch "Enhanced For Loop" New topic
Author

Enhanced For Loop

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi Guys,

I wrote the following code:



And when I run it, It givin result as

2
3
4
5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at EnhancedForLoop.main(EnhancedForLoop.java:14)


It's basically taking i value from index 1 and not from index 0. Am I doing something wrong?

Thanks.


Be Humble... Be Nice.
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

try

for(int i:a)
{
System.out.println(i);
}


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Thanks Ramesh. It worked.
ansuman mohapatra
Greenhorn

Joined: Mar 04, 2008
Posts: 27
even I m confused. Can any one clarify ?

and what does for(int i:a) does ??


Cleared SCJP.....
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

even I m confused. Can any one clarify ?

and what does for(int i:a) does ??


The for each loop , iterate over array "a" and takes each array element into temp variable i , so "i" contains the elements of array "a" starting from index 0.

The for each loop just saves your few lines of code and puts some readability :
The above code is same as this simple for loop :



Here the loop iterate through "a" array ...
[ June 30, 2008: Message edited by: Sagar Rohankar ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
ansuman mohapatra
Greenhorn

Joined: Mar 04, 2008
Posts: 27
ok but why it doesnt start from index 0 ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

It does, but the point is that i is not the index - it is the element of the array. Look carefully at what happens:

int[] a = { 1, 2, 3, 4, 5 };

First time in the loop: i = a[0] = 1 - Then you print out a[i] which is a[1] which is 2
Second time in the loop: i = a[1] = 2 - Then you print out a[i] = a[2] = 3
...
Fifth time in the loop: i = a[4] = 5 - Then you print out a[i] = a[5] -> ArrayIndexOutOfBoundsException because the array has only 5 members (a[0] to a[4]).

[ June 30, 2008: Message edited by: Jesper Young ]
[ June 30, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Your enhanced for loop did start at element 0. Element 0 is equal to 1. So, on the very first loop, i wil be set to 1. The next statement is the print where you ask it to print the value of a[i]. What is i? 1! So you picked up a[0], which was a one, and printed a[1] which is a two.


------------------------
Bob
SCJP - 86% - June 11, 2009
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi Rancher ,

you are using a good loop in bad way.


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Madhukar Ojha:
[Q]Hi Rancher ,

you are using a good loop in bad way.
[/Q]


Are you sure this code compile !!
 
wood burning stoves
 
subject: Enhanced For Loop