Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: How to not use casting to invoke the methods of a List of objects Date: Fri, 20 Jul 2012 18:03:08 +0200 Lines: 43 Message-ID: References: <61dca380-4364-4713-aa27-e885f96632ea@googlegroups.com> <1wvikyeajv4gw.1h99pyt4edcxc$.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 8AqilR48Ar6+ud4dFjjtRg5wgNHvMoMbnuq7aeio6m3GneKaaRgtOh9dgMyKdnzkI= Cancel-Lock: sha1:eOTIqW8muxhzMLVv6k4E5Mpp3hM= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: <1wvikyeajv4gw.1h99pyt4edcxc$.dlg@40tude.net> Xref: csiph.com comp.lang.java.programmer:16153 On 20.07.2012 17:45, Joerg Meier wrote: > On Fri, 20 Jul 2012 06:58:49 -0700 (PDT), clusardi2k@aol.com wrote: > >> The below start of replacement code fails because it skips through the list. There wasn't a good reason for me to finish the code! The loop can't even iterate the required number of loops. > >> Iterator it = objList.iterator(); > >> int size = objList.size(); > >> for (int i = 0;i < size; i++) >> { >> it.next(); >> } > > That is not really how you use iterators. Proper way: > > Iterator it = objList.iterator(); > > while (it.hasNext()) { > it.next(); > } In 2012 I'd rather say the proper way for a simple iteration (i.e. without removing elements or such) is for (final Route r : objList) { // camel case and accessor added: System.out.println(r.getLocationId()); } Assuming objList is assignment compatible to Iterable. We haven't seen the declaration in the original posting though so we can only speculate. I do have to agree that the original question was quite a bit dark. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/