• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

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: 23899
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
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic