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