• 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

for(Object name : collection type)

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
I came across this snippet and taught if you can give me some details for the below "for" loop:

for(Object name : v){
System.out.println(name);
}
say v is a vector. This type is used mainly in fileI/o Streams.
how exactly the loop is executed.

Thanks in advance
Balaji.S
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "enhanced for" loop is new with Java 5.0. Basically, for(Object name : v) means for each Object in v (where "name" is a variable referencing the object of that iteration).

This saves you the trouble of writing index code, like for(int x = 0; x < v.size(); x++), and then pulling out the element with something like v.get(x).
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kudos !! for the explanation..
Thanks
Balaji.S
reply
    Bookmark Topic Watch Topic
  • New Topic