Re: generics and arrays and multi-class collections



On Sat, 29 Sep 2007 22:36:13 -0700, SadRed <cardinal_ring@xxxxxxxxxxx>
wrote, quoted or indirectly quoted someone who said :

What part of the source do you refer?

code like this:

public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size,
a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}

If you write code like that I think you will get warnings.

It has been a while. I was trying to allocate arrays of the given
generic type, and was baffled. I used code similar to what I found in
Sun's collections and to my surprise discovered it generated warning
messages. I asked about it on the newsgroups and the wise ones
assured me this was indeed a limitation of Java's genericity design.

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
.



Relevant Pages

  • Re: generics and arrays and multi-class collections
    ... // Make a new array of a's runtime type, ... generic type, and was baffled. ... The code you've mentioned does generate an *unchecked* warning. ...
    (comp.lang.java.programmer)
  • Re: Java 5 Generics Question
    ... > The preceeding works with warnings. ... > What is the proper way to define an array of Vector? ... there is no runtime representation of the array element type ... type instance of a generic type. ...
    (comp.lang.java.programmer)
  • 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: Compiler warnings
    ... i followed your advice and no more warnings from the compiler. ... pointer, which points to 15 double* arrays, that each contain ... Since the parameters are statically defined, the array is most ...
    (comp.soft-sys.matlab)