问题类似于这个Why do we have to use an intermediary variable for @SuppressWarnings("unchecked")? ,但我无法从中找到解决方案。
作为练习,我从头开始构建哈希表结构。所以我写了一个类LinkedListDemo辅助HashTableDemo; LinkedListDemo 类测试绝对没问题。特别是,以下子例程在运行时不会导致 ClassCastException 错误:
public LinkedListDemo<S, T> reverse() {
if (count == 0)
return null;
@SuppressWarnings("unchecked")
S[] keys = (S[]) new Object[count];
@SuppressWarnings("unchecked")
T[] vals = (T[]) new Object[count];
ListNode<S, T> runner = head;
for (int k = 0; k < count; k++) {
keys[k] = runner.pair.getKey();
vals[k] = runner.pair.getValue();
runner = runner.next;
}
ArrayUtils.reverse(keys);
ArrayUtils.reverse(vals);
return new LinkedListDemo<S, T>(keys, vals);
}
而以下,在我的 HashTable 类中:
public class HashTableDemo<S, T> {
private LinkedListDemo<S, T>[] table = (LinkedListDemo<S, T>[]) new Object[10];
// more code...
}
有谁知道 Java 的 HashMap 类是如何规避这个问题的,和/或我该怎么做?我尝试按照上面链接中的建议在构造函数中创建一个中间变量 - 但它没有用。
最佳答案
这将编译:
@SuppressWarnings("unchecked") // this is okay because [insert reasoning here]
private LinkedListDemo<S, T>[] table =
(LinkedListDemo<S, T>[]) new LinkedListDemo<?, ?>[10];
您的代码错误的原因是 Object[]
不是 LinkedListDemo[]
,类似于 Object
的方式不是 LinkedListDemo
.
较早的未经检查的转换之所以有效,是因为 S[]
和 T[]
已erased至 Object[]
在运行时。
通常,不鼓励使用参数化类型的数组,因为它们的存在本身就是不安全的。这是因为:
- 数组是协变的 - a
LinkedListDemo<S, T>[]
是一个Object[]
.所以可以分配table
到Object[]
变量,然后分配一个String
作为一个元素——这在编译时是合法的,但至少会在运行时失败并返回ArrayStoreException
。 ,因为数组会进行运行时类型检查。 - 通用类型信息在运行时不可用,因为它已被编译器删除。可以再次分配
table
到Object[]
变量,但这次分配一个LinkedListDemo<String, Integer>
作为一个元素。在运行时,数组只会看到一个普通的LinkedListDemo
已添加,因此如果泛型类型错误,它不会立即失败。这可能会导致意外的ClassCastException
稍后再说。
看我的回答here有关此问题的更全面的解释和示例。另请参阅 Angelika Langer 的泛型常见问题解答的精彩解释:Can I create an array whose component type is a concrete parameterized type?
要点是,简单地使用 List<LinkedListDemo<S, T>>
会更容易和更安全。反而。如果您最终决定坚持使用 LinkedListDemo<S, T>[]
, 确保将其作为实现细节小心隐藏,并了解它可能被滥用的方式。
一些其他杂项注释:
-
@SuppressWarnings("unchecked")
除了抑制未经检查的强制转换编译器警告之外什么都不做。它本质上是在说“相信我,我知道我在做什么”——但你仍然需要小心,不要滥用未经检查的类型转换。有关详细信息,请参阅此帖子:How do I address unchecked cast warnings? - 与所有核心 API 类一样,source for
HashMap
可用,因此如果您想更好地了解其内部工作原理,您可以随时查看它。
关于Java 泛型 : Why does my @SuppressWarnings ("unchecked") work in this case, 而不是其他?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16879680/