Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

foreach: 1, private: 0

Thread view: 
Dimitri Maziuk - 15 Feb 2006 00:46 GMT
class foo {
private ArrayList<baz> data;
public Iterator<baz> iterator() { return data.iterator(); }
}

class bar {
public static void main( String [] args ) {
  for( baz : foo )
Error: foreach not applicable to expression type

Ok, lesse... "for( baz : foo.data )" compiles and runs. Remove
foo.iterator() -- still compiles and runs. Mind boggles.

Dima
Signature

The speed at which a mistyped command executes is directly proportional
to the amount of damage done.                                       -- Joe Zeff

Dimitri Maziuk - 15 Feb 2006 01:04 GMT
Dimitri Maziuk sez:
...

Looks like I jumped the gun there, it was fscking IDEA not rebuilding
the module correctly.

Still, it turns out you can't use foreach loop to iterate over a
private member list even if you have a public iterator. D'uh.

Dima
Signature

...the mainstream products of major vendors largely ignore these demonstrated
technologies...  [Instead, their customers] are left with several ineffective
solutions collected under marketing titles like "defense in depth".
        -- Thirty Years Later: Lessons from the Multics Security Evaluation

Thomas Hawtin - 15 Feb 2006 09:39 GMT
> Still, it turns out you can't use foreach loop to iterate over a
> private member list even if you have a public iterator. D'uh.

Did you want to implement Iterable?

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Ole Ildsgaard Hougaard - 15 Feb 2006 07:51 GMT
>class foo {
> private ArrayList<baz> data;
[quoted text clipped - 8 lines]
>Ok, lesse... "for( baz : foo.data )" compiles and runs. Remove
>foo.iterator() -- still compiles and runs. Mind boggles.

It sure boggles the mind that foo is a class and you're using it as a
variable. Please ask your questions using actual code.

To use values of type foo in a foreach loop, have foo implement
Iterable<baz>.
Dimitri Maziuk - 15 Feb 2006 16:55 GMT
...
> To use values of type foo in a foreach loop, have foo implement
> Iterable.

Thanks Ole and Thomas. It helps to read the fine JLS, not just
the "new features" blurb.

Dima
Signature

The most horrifying thing about Unix is that, no matter how many times you hit
yourself over the head with it, you never quite manage to lose consciousness.
It just goes on and on.                                  -- Patrick Sobalvarro



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.