jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "using Sets" Watch "using Sets" New topic

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()


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!


marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by samura babu:

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
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
It's not a secret anymore!