更新时间:2022-12-11 10:16:22
Guava 提供 Arrays.concat(T[], T[], Class
.
Class
参数的原因,仅供参考,是因为泛型数组有明显的上升趋势.如果你做了 Arrays.concat(Integer[], Long[])
,你想要返回一个 Object[]
吗?还是Number[]
?Guava 让你指定,所以没有歧义......而且因为所有替代方案都可能导致运行时不可预测的 ClassCastException
.
The reason for the Class
parameter, FYI, is because generic arrays have a distinct tendency to get upcast. If you did Arrays.concat(Integer[], Long[])
, did you want an Object[]
back? Or a Number[]
? Guava makes you specify so there's no ambiguity...and because all of the alternatives can lead to unpredictable ClassCastException
s at runtime.
(披露:我为 Guava 做出了贡献.)
(Disclosure: I contribute to Guava.)