This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes using Sets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "using Sets" Watch "using Sets" New topic
Author

using Sets

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111


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




marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
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.


SCJP-1.5<br />SCWCD-1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using Sets
 
Similar Threads
is LinkedHashSet ordered?
Problem faced in using hashset(comparison of equal elements)
Set question
for loop
Set.toArray