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

Discussion in 'Java' started by clusardi2k, Jul 20, 2012.

  1. clusardi2k

    clusardi2k Guest

    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 eveniterate 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,
     
    clusardi2k, Jul 20, 2012
    #1
    1. Advertisements

  2. clusardi2k

    markspace Guest


    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.

    Maybe you could try this:

    But in general the for-each construct works better

    for( Route r : objList ) {
    r. ...
    }
     
    markspace, Jul 20, 2012
    #2
    1. Advertisements

  3. clusardi2k

    clusardi2k Guest

    This works when I don't use the debugger to step through, thanks!
     
    clusardi2k, Jul 20, 2012
    #3
  4. clusardi2k

    Joerg Meier Guest

    That is not really how you use iterators. Proper way:

    Iterator <Route> it = objList.iterator();

    while (it.hasNext()) {
    it.next();
    }

    Liebe Gruesse,
    Joerg
     
    Joerg Meier, Jul 20, 2012
    #4
  5. 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
     
    Robert Klemme, Jul 20, 2012
    #5
  6. clusardi2k

    Joerg Meier Guest

    I agree that the enhanced for loop is preferrable for cases without
    structural changes to the list. Blame tunnel vision ;)

    Liebe Gruesse,
    Joerg
     
    Joerg Meier, Jul 20, 2012
    #6
  7. clusardi2k

    Roedy Green Guest

    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)
     
    Roedy Green, Jul 21, 2012
    #7
  8. clusardi2k

    Lew Guest

    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.
    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/
    In addition to what everyone else has said
    'objList' is a bad name. You have a collection of 'Route's, right? So your
    variable should be 'routes'.
    Why are you casting at all?

    Route route = routes.get(0);

    Indent enough?
    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, Jul 22, 2012
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.