N.D:"Anything worth having, takes time to get"
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
"I'm not back." - Bill Harding, Twister
N.D:"Anything worth having, takes time to get"
Originally posted by Xu Song:
The solution is obviously correct:Using the overloaded toArray(Object[]).But I still cannot understand your explanation.
Check Chapter5.4 of the Java Language Specification v2.0:That's the casting conversion rules.Since the elements of new array and the old one are legal to cast(Object can be casted to String),there's no error in the compile-time.In run-time,even if the aList is added with String elements,the ClassCastException still prompted.I'm confused by this runtime exception actually.
Originally posted by Joel McNary:
While it is true that String extends Object, it would not be true to say that String[] extends Object[].
Originally posted by Xu Song:
The code 1) has a ClassCastException at runtime.But the code 2) results in no exception at runtime.In this case,The casting conversion does not work with Object array only.Do I understand correctly?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |