1.5: generics warning of using Raw datatype

Discussion in 'Java' started by Thomas G. Marshall, Aug 17, 2004.

  1. The details of generics are currently something I'm not that comfortable
    with.

    Given the following code which is compiled using eclipse's 1.5 support:

    List list = new ArrayList();
    list.add("regular");

    I get this warning:

    Unsafe type operation: Should not invoke the
    method add(E) of raw type List. References to
    generic type List<E> should be parameterized

    But what if I really have no clue about the datatype I'm adding? Does this
    mean that I am encouraged by the JLS to use the following formalism:

    new ArrayList<? extends Object>()

    or similar?

    Thanks.


    --
    Everythinginlifeisrealative.Apingpongballseemssmalluntilsomeoneramsitupyourn
    ose.
     
    Thomas G. Marshall, Aug 17, 2004
    #1
    1. Advertising

  2. Thomas G. Marshall

    Paul Lutus Guest

    Thomas G. Marshall wrote:

    >
    > The details of generics are currently something I'm not that comfortable
    > with.
    >
    > Given the following code which is compiled using eclipse's 1.5 support:
    >
    > List list = new ArrayList();
    > list.add("regular");
    >
    > I get this warning:
    >
    > Unsafe type operation: Should not invoke the
    > method add(E) of raw type List. References to
    > generic type List<E> should be parameterized
    >
    > But what if I really have no clue about the datatype I'm adding?


    Tnen create a list of Objects, and cast each entry to the list as an Object.

    The entire point of the generic feature is to identify the type of the
    object, so this doesn't have to be done repeatedly elsewhere, in either
    writes to, or reads from, the list.

    Your application, in which you are adding unknown objects to a list, should
    parhaps be rethought anyway.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Aug 18, 2004
    #2
    1. Advertising

  3. Paul Lutus <> coughed up the following:
    > Thomas G. Marshall wrote:
    >
    >>
    >> The details of generics are currently something I'm not that
    >> comfortable with.
    >>
    >> Given the following code which is compiled using eclipse's 1.5
    >> support:
    >>
    >> List list = new ArrayList();
    >> list.add("regular");
    >>
    >> I get this warning:
    >>
    >> Unsafe type operation: Should not invoke the
    >> method add(E) of raw type List. References to
    >> generic type List<E> should be parameterized
    >>
    >> But what if I really have no clue about the datatype I'm adding?

    >
    > Tnen create a list of Objects, and cast each entry to the list as an
    > Object.
    >
    > The entire point of the generic feature is to identify the type of the
    > object, so this doesn't have to be done repeatedly elsewhere, in
    > either writes to, or reads from, the list.


    Well sure. I'd state it as type safety, but you're basically right.


    > Your application, in which you are adding unknown objects to a list,
    > should parhaps be rethought anyway.


    Well, no, I certainly am not putting this into any application. I'm trying
    to exercise the limits of the syntax.

    Is it the case then that

    new ArrayList();

    is no longer considered a kosher idiom, and that

    new ArrayList<Object>();

    is ok?



    --
    Forgetthesong,I'dratherhavethefrontallobotomy...
     
    Thomas G. Marshall, Aug 18, 2004
    #3
  4. Thomas G. Marshall

    Chris Smith Guest

    Thomas G. Marshall wrote:
    > Given the following code which is compiled using eclipse's 1.5 support:
    >


    Just a warning; Eclipse's 1.5 support, last I checked, was a bit
    questionable. I'd restrict myself to compiling sample 1.5 code with the
    Sun J2SDK for the time being, unless of course your intent is to help
    test the Eclipse compiler.

    > But what if I really have no clue about the datatype I'm adding? Does this
    > mean that I am encouraged by the JLS to use the following formalism:
    >
    > new ArrayList<? extends Object>()


    Actually, ArrayList<Object>. Wildcard types (eg, <? extends Object>)
    are valid as reference types, but not as classes, so you cannot use a
    wildcard in a new statement. Not to worry: ArrayList<Object> can
    contain references to subclasses of Object, so there's no point to the
    wildcard there anyway.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 18, 2004
    #4
  5. Chris Smith <> coughed up the following:
    > Thomas G. Marshall wrote:
    >> Given the following code which is compiled using eclipse's 1.5
    >> support:
    >>

    >
    > Just a warning; Eclipse's 1.5 support, last I checked, was a bit
    > questionable. I'd restrict myself to compiling sample 1.5 code with
    > the
    > Sun J2SDK for the time being, unless of course your intent is to help
    > test the Eclipse compiler.
    >
    >> But what if I really have no clue about the datatype I'm adding?
    >> Does this mean that I am encouraged by the JLS to use the following
    >> formalism:
    >>
    >> new ArrayList<? extends Object>()

    >
    > Actually, ArrayList<Object>. Wildcard types (eg, <? extends Object>)
    > are valid as reference types, but not as classes, so you cannot use a
    > wildcard in a new statement. Not to worry: ArrayList<Object> can
    > contain references to subclasses of Object, so there's no point to the
    > wildcard there anyway.


    Ah ok. I was confusing two things. Thanks. Been putting off ironing out
    generics for far too long...



    --
    Onedoctortoanother:"Ifthisismyrectalthermometer,wherethehell'smypen???"
     
    Thomas G. Marshall, Aug 18, 2004
    #5
  6. Thomas G. Marshall
    <> coughed up the
    following:
    > Paul Lutus <> coughed up the following:
    >> Thomas G. Marshall wrote:
    >>
    >>>
    >>> The details of generics are currently something I'm not that
    >>> comfortable with.
    >>>
    >>> Given the following code which is compiled using eclipse's 1.5
    >>> support:
    >>>
    >>> List list = new ArrayList();
    >>> list.add("regular");
    >>>
    >>> I get this warning:
    >>>
    >>> Unsafe type operation: Should not invoke the
    >>> method add(E) of raw type List. References to
    >>> generic type List<E> should be parameterized
    >>>
    >>> But what if I really have no clue about the datatype I'm adding?

    >>
    >> Tnen create a list of Objects, and cast each entry to the list as an
    >> Object.
    >>
    >> The entire point of the generic feature is to identify the type of
    >> the object, so this doesn't have to be done repeatedly elsewhere, in
    >> either writes to, or reads from, the list.

    >
    > Well sure. I'd state it as type safety, but you're basically right.
    >
    >
    >> Your application, in which you are adding unknown objects to a list,
    >> should parhaps be rethought anyway.

    >
    > Well, no, I certainly am not putting this into any application. I'm
    > trying to exercise the limits of the syntax.
    >
    > Is it the case then that
    >
    > new ArrayList();
    >
    > is no longer considered a kosher idiom, and that
    >
    > new ArrayList<Object>();
    >
    > is ok?


    n'mind. Chris Smith cleared up my confusion.

    --
    Onedoctortoanother:"Ifthisismyrectalthermometer,wherethehell'smypen???"
     
    Thomas G. Marshall, Aug 18, 2004
    #6
  7. "Thomas G. Marshall" <> wrote in message news:<3suUc.5246$zO3.3087@trndny05>...
    > The details of generics are currently something I'm not that comfortable
    > with.
    >
    > Given the following code which is compiled using eclipse's 1.5 support:
    >
    > List list = new ArrayList();
    > list.add("regular");
    >
    > I get this warning:
    >
    > Unsafe type operation: Should not invoke the
    > method add(E) of raw type List. References to
    > generic type List<E> should be parameterized
    >
    > But what if I really have no clue about the datatype I'm adding? Does this
    > mean that I am encouraged by the JLS to use the following formalism:
    >
    > new ArrayList<? extends Object>()
    >
    > or similar?


    I would use "new ArrayList<Object>()" just to show that I've made the
    decision that any object can be placed in the list. Using "new
    ArrayList()" you haven't specified what type of object that can/should
    be added to the list.

    /Jesper Nordenberg
     
    Jesper Nordenberg, Aug 18, 2004
    #7
  8. Thomas G. Marshall

    knightpraetor

    Joined:
    Jun 11, 2011
    Messages:
    1
    I'm still a little confused about how this works. So I am looking at someone else's code and eclipse complains that ArrayList is a raw type. It appears to be code to turn an arbitrary list of data into a string. What exactly does java want me to do?

    I just want to be sure, but are you saying that I need to make the list a list of objects and then whenever I place data in the list I should cast it to an object before putting it in. and then use a toString method on the object to return the string value?

    public static String listToString(ArrayList list) {
    String ret = "";
    if (list != null && list.size() > 0) {
    ret += "<";
    for (int i=0; i<(list.size()-1); i++) {
    ret += list.get(i) + ",";
    }
    ret += list.get(list.size()-1) + ">";
    }
    return ret;
    }
     
    knightpraetor, Jun 11, 2011
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jty202
    Replies:
    4
    Views:
    418
    Cor Ligthert
    Jan 18, 2005
  2. Keana
    Replies:
    0
    Views:
    344
    Keana
    Apr 22, 2004
  3. Jeff
    Replies:
    2
    Views:
    1,197
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Apr 16, 2007
  4. Royan
    Replies:
    8
    Views:
    772
    Patricia Shanahan
    Feb 15, 2008
  5. Replies:
    11
    Views:
    228
    glen herrmannsfeldt
    Dec 20, 2013
Loading...

Share This Page