• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

using Sets  RSS feed

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Hi There!






for(Object o:s)
System.out.print(o +" ");

may i know how the above code is represented in basic for loop?
there is no length attribute in set. instead, it has size()

for(declaration:expression)

expression must evaluate to the array to loop through. this could
be array variable or a method call that returns an array.
here 's' is not array. which is an set s = new Hashset();

how it works? correct me!



Thanks!
regards
samura




 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by samura babu:
...for(declaration:expression)

expression must evaluate to the array to loop through...


It does not need to be an array. The enhanced for statement can use either an array or any container that implements Iterable.

See JLS 14.14.2 The enhanced for statement.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are looking both the for loops differently. The basic for loop is for boolean (ba) that is declared as array. The enhanced for loop is for Set, because the type of collection(generic<> is not specified here, the things come out from Set is Object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!