Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > 1.5: generics warning of using Raw datatype

Reply
Thread Tools

1.5: generics warning of using Raw datatype

 
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-17-2004

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.Apingpongballseemssmal luntilsomeoneramsitupyourn
ose.


 
Reply With Quote
 
 
 
 
Paul Lutus
Guest
Posts: n/a
 
      08-18-2004
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

 
Reply With Quote
 
 
 
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-18-2004
Paul Lutus <(E-Mail Removed)> 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...


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      08-18-2004
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
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-18-2004
Chris Smith <(E-Mail Removed)> 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,wh erethehell'smypen???"



 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-18-2004
Thomas G. Marshall
<(E-Mail Removed). com> coughed up the
following:
> Paul Lutus <(E-Mail Removed)> 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,wh erethehell'smypen???"



 
Reply With Quote
 
Jesper Nordenberg
Guest
Posts: n/a
 
      08-18-2004
"Thomas G. Marshall" <(E-Mail Removed). com> 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
 
Reply With Quote
 
knightpraetor knightpraetor is offline
Junior Member
Join Date: Jun 2011
Posts: 1
 
      06-11-2011
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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
database datatype bit what is the C# datatype? Jeff ASP .Net 2 04-16-2007 08:26 PM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
Putting Raw formatted Data in proper datatype and displaying jty202 ASP .Net 4 01-18-2005 11:37 AM
DCOracle2 and long raw datatype Keana Python 0 04-22-2004 07:00 PM



Advertisments