Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #16146 > unrolled thread
| Started by | clusardi2k@aol.com |
|---|---|
| First post | 2012-07-20 06:58 -0700 |
| Last post | 2012-07-21 23:34 -0700 |
| Articles | 8 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | clusardi2k@aol.com |
|---|---|
| Date | 2012-07-20 06:58 -0700 |
| Subject | How to not use casting to invoke the methods of a List of objects |
| Message-ID | <61dca380-4364-4713-aa27-e885f96632ea@googlegroups.com> |
Below uses a List of objects of class Route. Class Route has public member variables (such as locationid) and public methods (such as get_locationid()). The below code is a first attempt at a way to obtain the value of locationid using casting. Question: What is the code to do it a better way.
((Route)objList.get(0)).locationid;
((Route)objList.get(1)).locationid;
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();
}
Thank you,
[toc] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-07-20 07:21 -0700 |
| Message-ID | <jubpi7$51p$1@dont-email.me> |
| In reply to | #16146 |
On 7/20/2012 6:58 AM, clusardi2k@aol.com wrote:
> The
> loop can't even iterate the required number of loops.
You might want to be more clear about what is really failing here.
Iterator works, so the problem must be somewhere else in the code, which
you are not showing.
>
> Iterator <Route> it = objList.iterator();
>
> int size = objList.size();
>
> for (int i = 0;i < size; i++)
> {
Maybe you could try this:
> Route r = it.next();
> }
But in general the for-each construct works better
for( Route r : objList ) {
r. ...
}
[toc] | [prev] | [next] | [standalone]
| From | clusardi2k@aol.com |
|---|---|
| Date | 2012-07-20 08:09 -0700 |
| Message-ID | <4751355c-c0e8-4c61-85af-747cbd6b2049@googlegroups.com> |
| In reply to | #16147 |
> On Friday, July 20, 2012 10:21:56 AM UTC-4, markspace wrote:
> > On 7/20/2012 6:58 AM, me wrote:
> > for (int i = 0;i < size; i++)
> > {
>
> Maybe you could try this:
>
> > Route r = it.next();
> > }
>
This works when I don't use the debugger to step through, thanks!
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2012-07-20 17:45 +0200 |
| Message-ID | <1wvikyeajv4gw.1h99pyt4edcxc$.dlg@40tude.net> |
| In reply to | #16146 |
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();
}
Liebe Gruesse,
Joerg
--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2012-07-20 18:03 +0200 |
| Message-ID | <a6tdqdF3eiU1@mid.individual.net> |
| In reply to | #16152 |
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/
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2012-07-20 18:59 +0200 |
| Message-ID | <10kadn1xvajk4$.cwzv0x5qw7.dlg@40tude.net> |
| In reply to | #16153 |
On Fri, 20 Jul 2012 18:03:08 +0200, Robert Klemme wrote:
> 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) { [...]
I agree that the enhanced for loop is preferrable for cases without
structural changes to the list. Blame tunnel vision ;)
Liebe Gruesse,
Joerg
--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-07-20 21:22 -0700 |
| Message-ID | <9hbk0892evf5rhp3p0verr6qhq8g447823@4ax.com> |
| In reply to | #16146 |
On Fri, 20 Jul 2012 06:58:49 -0700 (PDT), clusardi2k@aol.com wrote, quoted or indirectly quoted someone who said : >Below uses a List of objects of class Route. Class Route has public member = >variables (such as locationid) and public methods (such as get_locationid()= >). The below code is a first attempt at a way to obtain the value of locati= >onid using casting. Question: What is the code to do it a better way. see http://mindprod.com/jgloss/generics.html Generics let you tell the compiler what types you are hiding in your collections so it can do the casting for you. -- Roedy Green Canadian Mind Products http://mindprod.com The greatest shortcoming of the human race is our inability to understand the exponential function. ~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89) http://www.youtube.com/watch?v=F-QA2rkpBSY
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-07-21 23:34 -0700 |
| Message-ID | <jug6sq$mmi$1@news.albasani.net> |
| In reply to | #16146 |
On 07/20/2012 06:58 AM, clusardi2k@aol.com wrote:
> Below uses a List of objects of class Route. Class Route has public member
Public variables are frowned upon in Java. That doesn't mean never use them,
but it does make me wonder why you also defined an accessor method.
> variables (such as locationid) and public methods (such as get_locationid()).
You should follow the Java naming conventions: locationId and getLocationId().
Camel case. No underscores.
Don't describe your code. That's all but useless. Show your code.
http://sscce.org/
> The below code is a first attempt at a way to obtain the value of locationid
> using casting. Question: What is the code to do it a better way.
In addition to what everyone else has said
> ((Route)objList.get(0)).locationid;
'objList' is a bad name. You have a collection of 'Route's, right? So your
variable should be 'routes'.
> ((Route)objList.get(1)).locationid;
>
Why are you casting at all?
Route route = routes.get(0);
You did declare your list as a 'List<Route>', of course, right?
> 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();
Indent enough?
> int size = objList.size();
>
> for (int i = 0;i < size; i++)
> {
> it.next();
> }
You shouldn't use both indexes and iterators in the same loop. Stick with one
or another for any given loop.
You should never use an iterator 'next()' without checking 'hasNext()' first.
You often can, and therefore in those cases should, use the for-each loop, as
others have already said:
for (Route route : routes)
{
doSomethingWith(route);
}
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web