Re: Java 5 Generics Question



Steven <steven.green30@xxxxxxxxxxx> writes:

> I am trying to define an array of vectors.
> For example:
>
> Vector<Object>[] data = new Vector[4];
>
> The preceeding works with warnings.
> Trying:
>
> Vector<Object>[] data = new Vector<Object>[4];
>
> Fails with errors.
> What is the proper way to define an array of Vector<Object> ?

You cannot (in Java 5.0). The reason is that there's no way the array
element assignment check at data[i] = ... could be performed, because
there is no runtime representation of the array element type
Vector<Object>.

Instead use array lists, as in:

ArrayList<Vector<Object>> data = new ArrayList<Vector<Object>>();

WARNING: Technical information, not flame bait, ahead:

This is one of the things that work a little better in
C#/CLI 2.0: the runtime supports exact type instances of generic
types, and therefore you can create an array whose element type is a
type instance of a generic type.

Peter
--
Department of Natural Sciences http://www.dina.kvl.dk/~sestoft/
Royal Veterinary and Agricultural University * Tel +45 3528 2334
Thorvaldsensvej 40, DK-1871 Frederiksberg C, Denmark * Fax +45 3528 2350
.



Relevant Pages

  • Re: K&R2 section 2.7 type conversions (exercise)
    ... warnings), ... we are asked to write a function "htoi(an array)" that accomplishes ... value of type char. ... int main{ ...
    (comp.lang.c)
  • Re: matching password problem
    ... and turn on warnings. ... You don't need to store the usernames and passwords in arrays in your code, because you only need to look at ONE username and ONE password at a time. ... And if you DID want to store them, you should use a hash, not an array. ... use strict; use warnings; ...
    (perl.beginners)
  • Re: Question on loops and return values
    ... You should always enable warnings when developing Perl code! ... > associated with an array, but I could be wrong on that too. ... It cannot be clobbered since your program never writes to that filehandle. ...
    (comp.lang.perl.misc)
  • Re: Module questions (perhaps a module creation mini-tutorial)
    ... > use warnings; ... > use strict; ... You shouldn't be using this array. ... > package I am calling. ...
    (perl.beginners)
  • Re: random numbers
    ... > These numbers must match to the number of an array. ... is better because it allows you to turn warnings for selected lexical ... Please do read the posting guidelines for this group tolearn how you can ... Sinan Unur ...
    (comp.lang.perl.misc)