问题描述
是新的堆栈<> [N]
等同于新的堆栈[N]
的通用数据键入堆栈<项目>
编辑:虽然我明白,混合泛型类型,应该最好避免阵列和更强大的解决方案的存在,我的查询仍然有效:广泛认可的教科书如的算法,第4版的凯文·韦恩和罗伯特·塞奇威克(第158)建议使用结构如下所示:
堆栈<串GT; A =(堆栈<串GT; [])新的堆栈[N];
是的,他们是等价的,除了返回类型(其中一个是堆栈[]
,其他为堆栈<方式> []
)
注意堆栈[]
可分配给堆栈<弦乐> []
没有一个明确的转换(您只会得到一个unchecked转换警告),而它分配一个堆栈℃的误差;> []
到堆栈<弦乐> []
不进行强制转换。
新的堆栈[N]
使用不是在新的code推荐原始类型。
Is new Stack<?>[N]
equivalent to new Stack[N]
for a generic data type Stack<Item>
?
EDIT: While I understand that mixing generic types and arrays should best be avoided and that more robust solutions exist, my query still stands: widely recognized textbooks such as Algorithms, 4th Edition by Kevin Wayne and Robert Sedgewick (pg. 158) suggest using constructs like the following:
Stack<String>[] a = (Stack<String>[]) new Stack[N];
Yes, they are equivalent, except for the return type (one is Stack[]
, the other is Stack<?>[]
).
Note that Stack[]
can be assigned to Stack<String>[]
without an explicit cast (you will just get an unchecked conversion warning), whereas it is an error to assign a Stack<?>[]
to Stack<String>[]
without a cast.
new Stack[N]
uses a raw type which is not recommended in new code.
这篇关于爪哇 - 是新的堆叠式和LT;&GT; [N]等同于新的堆栈[N]泛型数据类型栈&LT;项目&GT ;??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!