[英]I know that generic arrays are not supported by Java, but I don't know how to fix this so it will work
我正在尝试创建一个HashTable,代码的这一部分需要一个数组,但是当它不被泛型泛型时,我会得到未经检查的警告,但是我知道不支持泛型数组,但是我不确定如何解决这个问题。 。
array = new HashEntry<AnyType>[ nextPrime( arraySize ) ];
I suggest checking out JDK's own code for HashMap
, specifically the resize
method and these lines: 我建议检查一下JDK自己的HashMap
代码,特别是resize
方法和以下几行:
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
newTab
is then assigned to the main instance variable, table
. 然后将newTab
分配给主实例变量table
。 So, if JDK can't avoid @SuppressWarnings
, neither will you. 因此,如果JDK无法避免@SuppressWarnings
,那么您也不会。
Write it as such 这样写
@SuppressWarnings("unchecked")
HashEntry<K, V>[] array = new HashEntry[nextPrime()];
then add @SuppressWarnings("unchecked") to it. 然后向其添加@SuppressWarnings(“ unchecked”)。
Don't use arrays. 不要使用数组。 Use collections. 使用集合。
Using arrays requires more code, more care and gives no measurable benefit. 使用数组需要更多的代码,需要更多的照顾,并且无法带来可观的收益。
Using collections leverages the code and care built into the JDK. 使用集合利用了JDK中内置的代码和关怀。
If you've got an array of HashEntry, you're most of the way to a Map - just use a constant order map: 如果您有一个HashEntry数组,那么就很容易使用Map-只需使用恒定顺序的map:
Map<SomeKey, SomeValue> map = new LinkedHashMap<SomeKey, SomeValue>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.