Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

howto create an array with parameterized type

Thread view: 
Ulrich Scholz - 27 Jan 2006 10:37 GMT
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;
   }
Torkel Franzen - 27 Jan 2006 10:48 GMT
> 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.
Robert Klemme - 27 Jan 2006 10:51 GMT
>> 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.

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
Roedy Green - 27 Jan 2006 10:48 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Weidenfeller - 27 Jan 2006 11:23 GMT
>         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
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Thomas Hawtin - 27 Jan 2006 17:03 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Ulrich Scholz - 27 Jan 2006 18:14 GMT
> 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?

Ulrich
John C. Bollinger - 28 Jan 2006 03:47 GMT
>>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.

Signature

John Bollinger
jobollin@indiana.edu



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.