• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

problem with enhanced for loop

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler error?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23923
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

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic