howto create an array with parameterized type

Discussion in 'Java' started by Ulrich Scholz, Jan 27, 2006.

  1. 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;
    }
    Ulrich Scholz, Jan 27, 2006
    #1
    1. Advertising

  2. "Ulrich Scholz" <> writes:

    > 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.
    Torkel Franzen, Jan 27, 2006
    #2
    1. Advertising

  3. Ulrich Scholz

    Roedy Green Guest

    On 27 Jan 2006 02:37:02 -0800, "Ulrich Scholz" <> wrote,
    quoted or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 27, 2006
    #3
  4. Torkel Franzen wrote:
    > "Ulrich Scholz" <> writes:
    >
    >> 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
    Robert Klemme, Jan 27, 2006
    #4
  5. Ulrich Scholz wrote:
    >
    > 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
    --
    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 Weidenfeller, Jan 27, 2006
    #5
  6. Ulrich Scholz wrote:
    >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Jan 27, 2006
    #6
  7. > 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
    Ulrich Scholz, Jan 27, 2006
    #7
  8. Ulrich Scholz wrote:
    >>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.


    --
    John Bollinger
    John C. Bollinger, Jan 28, 2006
    #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. Replies:
    2
    Views:
    1,979
  2. Replies:
    2
    Views:
    399
    Roedy Green
    Feb 15, 2006
  3. Replies:
    2
    Views:
    345
    Soren Kuula
    Aug 24, 2006
  4. cdvr
    Replies:
    4
    Views:
    1,060
    Piotr Kobzda
    Jul 22, 2007
  5. Lambda
    Replies:
    0
    Views:
    512
    Lambda
    Apr 27, 2008
Loading...

Share This Page