Re: generics and arrays and multi-class collections



On Sep 30, 4:08 pm, Roedy Green <see_webs...@xxxxxxxxxxxxxxxxxxxx>
wrote:
On Sat, 29 Sep 2007 22:36:13 -0700, SadRed <cardinal_r...@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 Glossaryhttp://mindprod.com

Yes. The code you've mentioned does generate an *unchecked* warning.
Sun has just ignored it just like us. :):)

.



Relevant Pages

  • Re: generics and arrays and multi-class collections
    ... // Make a new array of a's runtime type, ... If you write code like that I think you will get warnings. ... generic type, and was baffled. ...
    (comp.lang.java.programmer)
  • Re: fgets() - supposed to be simple :)
    ... shouldn't) use the macro with a semicolon. ... And you need an array of 100 floats for the amount of the purchase? ... >void menu; ... This is an error, not a warning. ...
    (comp.lang.c)
  • Re: Array of Pointers
    ... complex) data structure than a simple array to do this efficiently. ... int mainor int main ... bigarray.c:6: warning: ISO C89 forbids variable-size array `arrayofpointers' ...
    (comp.lang.c)
  • Re: Are constants not locally static?
    ... array length of the value it's allowed (but not compliant, ... yada yada ... ... So onto the code that caused the warning. ... The package is ...
    (comp.lang.vhdl)
  • Re: HELP! Trying to add Email Notification to Download File Code
    ... This warning will go away once you get the headers sorted out. ... Invalid argument supplied for foreachin ... # verifies if the $filelist array has an element ... # builds an array with all valid files in the ...
    (comp.lang.php)