Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: supercalifragilisticexpialadiamaticonormalizeringelimatisticantations Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder Difficulties Date: Wed, 06 Jul 2011 19:52:38 -0400 Organization: supercalifragilisticexpialadiamaticonormalizeringelimatisticantations Lines: 23 Message-ID: References: <97hd9sFa1jU2@mid.individual.net> <97jiioFr6pU2@mid.individual.net> NNTP-Posting-Host: 9zMfMLj7Kt4oubZ/fw1Ukg.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: WinVN 0.99.12z (x86 32bit) X-Notice: Filtered by postfilter v. 0.8.2 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5928 On 06/07/2011 12:59 PM, blmblm@myrealbox.com wrote: > // iterate over all elements of s -- this "foreach" syntax > // is possible AFAIK with all classes that implement Collection > for (Foo foo : s) { > // note use of C-like printf > System.out.printf("foo with value %d, hashcode %d\n", > foo.n, foo.hashCode()); > } In fact, it's possible with all classes that implement Iterable; the Collection interface extends Iterable so you are correct, but it goes further than just Collection. The only classes in java.* and javax.* that implement it seem to implement Collection (you'd think CharSequence as well, and perhaps even Map and/or Enumeration or all the legacy classes that provide an Enumeration, but no), but third party libraries can in principle implement it separately. The foreach syntax can also be used with arrays, including arrays of unboxed primitives, even though using reflection on an array (e.g. (new Object[3]).getClass().getInterfaces();) only shows it implementing Cloneable and Serializable, not Iterable.