爪哇 - 是新的堆叠式和LT;> [N]等同于新的堆栈[N]泛型数据类型栈<项目&GT ;?? [英] Java - Is new Stack<?>[N] equivalent to new Stack[N] for generic data type Stack<Item>?

查看:88
本文介绍了爪哇 - 是新的堆叠式和LT;> [N]等同于新的堆栈[N]泛型数据类型栈<项目&GT ;??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的堆栈<> [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 ;??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆