howto create an array with parameterized type

U

Ulrich Scholz

Dear all,

I have legacy code which uses an array with parameterized components.
Below you find an example. Although it works, eclipse gives me a
warning: "Type safety: The expression of type Vector[] needs unchecked
conversion to conform to Vector<Fact>[]".

I tried to dig in the topic, e.g., by reading generics in the Java
Programming Language by Gilda Bracha. But I have to admit: I didn't
get it fully. How to get rid of the warning without massive changes on
the legacy code?

Thank you,

Ulrich


public static Vector<Fact>[] convertState(Vector<JSHOP2_Term>[]
stateJSHOP2)
{
Vector<Fact>[] statePPlanner = new Vector[nbPredicates]; //
gives warining

for(int head=0; head < nbPredicates; head++)
{
// do something
}

return statePPlanner;
}
 
T

Torkel Franzen

Ulrich Scholz said:
How to get rid of the warning without massive changes on
the legacy code?

You can't. You'll just need to convince yourself that the code works
OK in spite of the warning.
 
R

Roedy Green

How to get rid of the warning without massive changes on
the legacy code?

it is not that many lines, just the line that says Vector x = new
Vector( n );

and your Comparators and Comparables. Once you have done a few, you
can just crank them out mindlessly without thinking. You don't have
to remove the unneeded casts.

See http://mindprod.com/jgloss/generics.html
http://mindprod.com/jgloss/comparator.html
http://mindprod.com/comparable.html

for some recipes for the common generifying tasks.
 
R

Robert Klemme

Torkel said:
You can't. You'll just need to convince yourself that the code works
OK in spite of the warning.

Additionally one can do

@SuppressWarnings("unchecked")
public static Vector<Fact>[] convertState(Vector<JSHOP2_Term>[]
stateJSHOP2) {
Vector<Fact>[] statePPlanner = new Vector[nbPredicates]; // no warning
any more
....
}

robert
 
T

Thomas Weidenfeller

Ulrich said:
Vector<Fact>[] statePPlanner = new Vector[nbPredicates]; //
gives warining

You are running into the fundamental problem that the semantics of
arrays didn't fit well into Sun's generics hack when they bolted the
generic stuff onto the language. There are no arrays of a concrete
parametrized type (IMHO one of the many reasons why the Java generics
are such a stupid idea).

Instead of having an array of Vectors you might want to consider having
a Vector of Vectors (or an ArrayList of ArrayLists). This would of
course require changes in your other code.

Alternatively, you could try an unbound wildcard as the type parameter
(Vector<?>), but then you don't have the guarantee that only
Vector<Fact> objects are in the array.

/Thomas
 
T

Thomas Hawtin

Ulrich said:
I have legacy code which uses an array with parameterized components.
Below you find an example. Although it works, eclipse gives me a
warning: "Type safety: The expression of type Vector[] needs unchecked
conversion to conform to Vector<Fact>[]".

In modern Java there is little reason to use arrays of objects of any
type. Avoid clinging on to obsolete techniques and use List<List<Fact>>.

Tom Hawtin
 
U

Ulrich Scholz

In modern Java there is little reason to use arrays of objects of any

You are not referring to int[], or are? If you are, why that?
References?

Ulrich
 
J

John C. Bollinger

Ulrich said:
In modern Java there is little reason to use arrays of objects of any
type.


You are not referring to int[], or are? If you are, why that?
References?

Java ints are primitives, not objects. I believe Tom's point was that
ignoring legacy issues, type-parameterized Lists give you >95% of what
you might want from arrays of objects, plus some useful capabilities
that arrays don't give you.
 

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

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top