File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with 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 "problem with enhanced for loop" Watch "problem with enhanced for loop" New topic

problem with enhanced for loop

mambe nanje
Ranch Hand

Joined: Feb 22, 2006
Posts: 31
check this out:

class Sample{
public static void main(String[] args){
int[] nums={1,2,3,4};
for(int i : nums)
for(int j : nums)
System.out.println(i+" "+j+" ");

what will be the outcome of the

Da Clone in programming world
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Compiler error?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ashley Waldrop

Joined: Feb 27, 2006
Posts: 9
I think you missed "}" in the end. If we have "}" in the end then this program will compile and should produce below output(Not in this format )

i=1 and j =1,2,3,4
i=2 and j =1,2,3,4
i=3 and j =1,2,3,4
i=4 and j =1,2,3,4
sreedhar lak
Ranch Hand

Joined: Feb 26, 2006
Posts: 42
Sorry,I am getting compile time error

---------- compile ---------- ';' expected
for(int i : nums)
^ illegal start of expression
2 errors

My Jdk version is :"j2sdk1.4.2_08"
please,tell me what is enhanced for loop
Thanks a lot.

Thanks a lot<br />Learn from yesterday, live for today, hope for tomorrow. The important thing is to not stop questioning
Henry Wong

Joined: Sep 28, 2004
Posts: 20530

My Jdk version is :"j2sdk1.4.2_08"
please,tell me what is enhanced for loop
Thanks a lot.

The "enhanced for" loop, being discussed in this thread, is a variant of the "for" loop, that was added into Java 5. It basically is more of a foreach type of loop -- smart enough to either loop over an array, or via an iterator, automatically.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: problem with enhanced for loop
It's not a secret anymore!