1.5: generics warning of using Raw datatype

T

Thomas G. Marshall

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.
 
P

Paul Lutus

Thomas said:
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.
 
T

Thomas G. Marshall

Paul Lutus said:
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?
 
C

Chris Smith

Thomas said:
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
 
T

Thomas G. Marshall

Chris Smith said:
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.


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...
 
T

Thomas G. Marshall

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



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.
 
J

Jesper Nordenberg

Thomas G. Marshall said:
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
 
Joined
Jun 11, 2011
Messages
1
Reaction score
0
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;
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top