#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0

    method chaining and casts


    I was wondering if anyone knows whether it is possible to somehow apply method chaining on an object that requires a cast.

    For example:
    Code:
    ArrayList ar = new ArrayList();
    ar.add(new Integer(3));
    int i = ar.get(0).intValue();
    generates a compiler error.

    To get the int value from the ArrayList above I have to do the following:
    Code:
    ArrayList ar = new ArrayList();
    ar.add(new Integer(3));
    Integer intObject = (Integer)ar.get(0);
    int i = intObject.intValue();
    Is it at all possible to get the int value somehow without explicitly creating an Integer object? Is there a possible way to combine method chaining and casting in this case?
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Yes, you can. It can be kind of ugly, but I still use it at times:
    Code:
    ArrayList ar = new ArrayList();
    ar.add(new Integer(3));
    int i = ((Integer)ar.get(0)).intValue();

IMN logo majestic logo threadwatch logo seochat tools logo