Re: ClassCastException on Array content cast
Bill McCleary wrote:
Lew wrote:
RVic wrote:
Answered my own q [sic] -- in case anyone hits this in the future on a
search, should be:
Given that the two forms of toArray() are document right adjacent to
each other in the Javadocs, it shouldn't be hard to find one given the
other.
arrayList.toArray(dmFieldInfos); //provided dmFieldInfos is th correct
length
dmFieldInfos can be any length. It is common to see this idiom as:
DmFieldInfo [] dmFieldInfos = arrayList.toArray( new DmFieldInfo [0] );
How cheap is the creation and discarding of the zero-length array here?
My guess would be decently cheap with a modern GC and especially if,
after using it for type inference, the compiler sees that it's never
used and optimizes it away. (If not javac, the JIT.)
See Thomas Pornin's response. If you're still fretful,
DmFieldInfo[] dmFieldInfos =
arrayList.toArray( new DmFieldInfo[ arrayList.size() ] );
--
Eric.Sosman@sun.com