• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with enhanced for loop

 
mambe nanje
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler error?
 
Ashley Waldrop
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,I am getting compile time error

---------- compile ----------
Sample.java:4: ';' expected
for(int i : nums)
^
Sample.java:7: 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic