• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in for loop

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(Integer i : int){}

i could not understand this for loop structure.

i have seen only this format : for( ; ; )

please explain.

is this new in 5.0?

any other for loop structure is existing?

need help.

ARUL JOSE.
[ February 28, 2005: Message edited by: Jim Yingst ]
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In one of your other posts you indicate you have Java 1.5 Tiger, A developers notebook - by, Brett McLaughlin and David Flanagan.

From Table of Contents: Chapter 7. The for/in Statement

See Tiger enhancements to java programming language.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yes Arul , this is new in 1.5 .

I am slightly modifying your code like this :


what is happening here is , intArray is an array of type int . This loop will automatically iterate this array & put the current value in i ( left side variable ) ....

Note :
1] your right hand side variable should have the values that is compatable to left hand side variable .
2] your right hand variable should be an array or iterator .

ofcourse , you can have code like this



But in the following example , I am not sure that it compiles fine or not , can any body confirm ..



[ March 01, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Arul , this is new in 1.5 .

I am slightly modifying your code like this so that it doesn't compile:



what is happening here is , intArray is an array of type int actually nothing is happening because it won't compile. This loop will automatically iterate this array & put the current value in i ( left side variable ) ....

Note :
1] your right hand side variable should have the values that is compatable implicitly assignable to left hand side variable .
2] your right hand variable should be an array or iterator An array or instance of java.lang.Iterable.


ofcourse , you can have code like this if you want to test that the compiler will fail it


int intArray = {1,2,3,4,5};

for(Integar i : intArray) { // boxing
System.out.print(i+" ");
}

*snip other stuff that won't compile*

Dude, misleading is worse than saying nothing at all.
You and the OP need to head to http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh .. I am sorry ,
I forgot [] at the time of array declaration ...

Tony , Is there any other mistake in the codes , can you please mention ...

Thanks a lot Tony .
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only other mistake in the code is "Integar".
The explanations are incorrect and/or misleading though.
reply
    Bookmark Topic Watch Topic
  • New Topic