Generic Array Creation

Discussion in 'Java' started by Marc van Dongen, Feb 5, 2009.

  1. Dear all,


    I'd very much appreciate if somebody could provide a pointer on how to
    do the following.

    I've defined a generic interface MyInterface<T> and several classes
    that implement this interface. I'd like to create an array the members
    of which can be assigned any object belonging to a class that
    implements the interface.

    I know that an Object array would solve the problem of assigning
    objects from different classes. However, this is not very clean as I'd
    lose type information and I'd have to cast each time I get something
    from the array.

    I've considered java.lang.reflect.Array.newInstance( Class C, int
    size ) but this requires a single class C and I'd end up with an
    array which only allows me assignments of C subclass objects.

    Ideally, I'd like to declare the array as follows: MyInterface<Type>[]
    array, for a specific raw type.

    Is there a way to do this?

    Thanks in advance for your help.

    Regards,


    Marc van Donge
     
    Marc van Dongen, Feb 5, 2009
    #1
    1. Advertising

  2. In article
    <>,
    Marc van Dongen <> wrote:

    [...]
    > I'd very much appreciate if somebody could provide a pointer on how
    > to do the following.
    >
    > I've defined a generic interface MyInterface<T> and several classes
    > that implement this interface. I'd like to create an array the
    > members of which can be assigned any object belonging to a class that
    > implements the interface.
    >
    > I know that an Object array would solve the problem of assigning
    > objects from different classes. However, this is not very clean as
    > I'd lose type information and I'd have to cast each time I get
    > something from the array.
    >
    > I've considered java.lang.reflect.Array.newInstance( Class C, int
    > size ) but this requires a single class C and I'd end up with an
    > array which only allows me assignments of C subclass objects.
    >
    > Ideally, I'd like to declare the array as follows:
    > MyInterface<Type>[] array, for a specific raw type.
    >
    > Is there a way to do this?


    In chapter 5, item 25, of _Effective_Java_, Joshua Bloch discusses why
    this is not type safe:

    <http://java.sun.com/docs/books/effective/>

    Subsequent items suggest alternatives.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Feb 5, 2009
    #2
    1. Advertising

  3. Marc van Dongen <> wrote:
    > I've defined a generic interface MyInterface<T> and several classes
    > that implement this interface. I'd like to create an array the members
    > of which can be assigned any object belonging to a class that
    > implements the interface.


    arrays and generics do not work together with current versions of Java
    (for any sufficiently interesting definition of "work together")

    You can only define arrays for non-generic types, and do casts
    in all the accessors.
     
    Andreas Leitgeb, Feb 5, 2009
    #3
  4. Marc van Dongen

    Mark Space Guest

    Marc van Dongen wrote:
    >
    > Ideally, I'd like to declare the array as follows: MyInterface<Type>[]
    > array, for a specific raw type.
    >
    > Is there a way to do this?


    Syntactically this works:

    List<?>[] listA = new ArrayList<?>[3];

    Which is basically the same that you'd get at runtime anyway. If you
    need to do things with this array, it might help if we new what those
    things were....
     
    Mark Space, Feb 5, 2009
    #4
  5. On Feb 5, 1:28 pm, "John B. Matthews" <> wrote:
    [Generic Arrays]

    > <http://java.sun.com/docs/books/effective/>
    >
    > Subsequent items suggest alternatives.


    Thank you. I'll hava a look at it.

    Regards,


    Marc van Dongen
     
    Marc van Dongen, Feb 6, 2009
    #5
  6. On Feb 5, 2:08 pm, Thomas Pornin <> wrote:
    > According to Marc van Dongen  <>:


    [ Generic Arrays ]

    > Type parameters exist only at compile-time. At runtime, the array
    > instance will "know" that it is an array of "MyInterface", but it cannot
    > know anything like "MyInterface<Type>". What you can do is the following:
    >
    >    MyInterface<? extends Type>[] t = new MyInterface[n];


    Thanks. This comment actually made me find a solution which I hadn't
    thought of. When I tried it it worked straight away. I hadn't thought
    of (1) declaring an array of an unparameterised type MyInterfaces,
    filling it, and then casting it to a parameterised type. This solution
    is quite acceptable to me as the cast is the only one in my program.

    [more stuff]

    Thanks.

    >     MyInterface[] t = new MyInterface[n];
    >     Object[] u = t;
    >     u[0] = "Hello World!";


    Thanks. I'm aware of this and it won't happen. The array is filled
    once and remains fixed. That's why I prefer using arrays to a generic
    list type. I had tried the generic list route but it is *much* (IIRC
    4-5 times) slower.

    > A last note on the compiler "guarantee": as you see, the warning warns
    > you about a runtime ClassCastException which may happen, but not
    > necessarily in the same class. The compiler guarantee is complete only
    > if you compiled _all_ the classes comprising your application (and it
    > can be fooled nonetheless with reflection).


    I compiled all classes, got no errors and only one warning.

    [use native code]

    Thanks very much for your detailed answer. It helped me solve the
    problem.

    Regards,


    Marc van Dongen
     
    Marc van Dongen, Feb 6, 2009
    #6
  7. On Feb 5, 2:10 pm, Andreas Leitgeb <>
    wrote:

    [ Generic Arrays ]

    > arrays and generics do not work together with current versions of Java
    > (for any sufficiently interesting definition of "work together")
    >
    > You can only define arrays for non-generic types, and do casts
    > in all the accessors.


    Thanks.

    Regards,


    Marc van Dongen
     
    Marc van Dongen, Feb 6, 2009
    #7
  8. On Feb 5, 6:28 pm, Mark Space <> wrote:

    [Generic Array Creation]

    > Syntactically this works:
    >
    >    List<?>[] listA = new ArrayList<?>[3];


    Thanks. I'm not sure if I understand you point. If you're suggesting I
    should use lists, then I could but they're too slow for me.

    > Which is basically the same that you'd get at runtime anyway. If you
    > need to do things with this array, it might help if we new what those
    > things were....


    Reading only.

    Regards,


    Marc van Dongen
     
    Marc van Dongen, Feb 6, 2009
    #8
  9. Marc van Dongen

    Mark Space Guest

    Marc van Dongen wrote:
    > On Feb 5, 6:28 pm, Mark Space <> wrote:
    >
    > [Generic Array Creation]
    >
    >> Syntactically this works:
    >>
    >> List<?>[] listA = new ArrayList<?>[3];

    >
    > Thanks. I'm not sure if I understand you point. If you're suggesting I
    > should use lists, then I could but they're too slow for me.


    No I'm using List because it's an interface that everybody has and has
    access to. You can substitute "MyInterface" for "List" and "ArrayList"
    up there and it will work the same.
     
    Mark Space, Feb 6, 2009
    #9
  10. Marc van Dongen

    Lew Guest

    Marc van Dongen wrote:
    > On Feb 5, 2:08 pm, Thomas Pornin <> wrote:
    >> According to Marc van Dongen <>:

    >
    > [ Generic Arrays ]
    >
    >> Type parameters exist only at compile-time. At runtime, the array
    >> instance will "know" that it is an array of "MyInterface", but it cannot
    >> know anything like "MyInterface<Type>". What you can do is the following:
    >>
    >> MyInterface<? extends Type>[] t = new MyInterface[n];

    >
    > Thanks. This comment actually made me find a solution which I hadn't
    > thought of. When I tried it it worked straight away. I hadn't thought
    > of (1) declaring an array of an unparameterised type MyInterfaces,
    > filling it, and then casting it to a parameterised type. This solution
    > is quite acceptable to me as the cast is the only one in my program.


    Make sure you follow the guidelines from /Effective Java/, referenced
    upthread, with respect to use of '@SuppressWarnings( "unchecked" )'.

    --
    Lew
     
    Lew, Feb 7, 2009
    #10
    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. Murat Tasan
    Replies:
    1
    Views:
    8,097
    Chaitanya
    Feb 3, 2009
  2. Mike Schilling

    Type-safe array creation

    Mike Schilling, May 13, 2006, in forum: Java
    Replies:
    5
    Views:
    574
    Mike Schilling
    May 13, 2006
  3. Replies:
    2
    Views:
    460
  4. minlearn
    Replies:
    2
    Views:
    477
    red floyd
    Mar 13, 2009
  5. Chris Peeters

    generic web page creation ???

    Chris Peeters, Jun 29, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    291
    Chris Peeters
    Jun 29, 2009
Loading...

Share This Page