Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #16153

Re: How to not use casting to invoke the methods of a List of objects

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: How to not use casting to invoke the methods of a List of objects
Date 2012-07-20 18:03 +0200
Message-ID <a6tdqdF3eiU1@mid.individual.net> (permalink)
References <61dca380-4364-4713-aa27-e885f96632ea@googlegroups.com> <1wvikyeajv4gw.1h99pyt4edcxc$.dlg@40tude.net>

Show all headers | View raw


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 <Route> 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 <Route> 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<Route>.  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/

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to not use casting to invoke the methods of a List of objects clusardi2k@aol.com - 2012-07-20 06:58 -0700
  Re: How to not use casting to invoke the methods of a List of objects markspace <-@.> - 2012-07-20 07:21 -0700
    Re: How to not use casting to invoke the methods of a List of objects clusardi2k@aol.com - 2012-07-20 08:09 -0700
  Re: How to not use casting to invoke the methods of a List of objects Joerg Meier <joergmmeier@arcor.de> - 2012-07-20 17:45 +0200
    Re: How to not use casting to invoke the methods of a List of objects Robert Klemme <shortcutter@googlemail.com> - 2012-07-20 18:03 +0200
      Re: How to not use casting to invoke the methods of a List of objects Joerg Meier <joergmmeier@arcor.de> - 2012-07-20 18:59 +0200
  Re: How to not use casting to invoke the methods of a List of objects Roedy Green <see_website@mindprod.com.invalid> - 2012-07-20 21:22 -0700
  Re: How to not use casting to invoke the methods of a List of objects Lew <noone@lewscanon.com> - 2012-07-21 23:34 -0700

csiph-web