java中实现HashMap时的java.lang.ClassCastException
当我使用java中实现HashMap时的java.lang.ClassCastException,java,hashmap,Java,Hashmap,当我使用MapEntry[]entry=(MapEntry[])新对象[capacity]时 它告诉我java.lang.ClassCastException 这怎么会发生?我对此感到困惑。(因为它是通用的,所以我似乎应该做演员) 我找到了一些教程,他们是这样使用的: table = new Entry[capacity]; () 甚至连演员都没有 我的代码如下 public class MyHashMap<K, V> { private class MapEntry {
MapEntry[]entry=(MapEntry[])新对象[capacity]
时
它告诉我java.lang.ClassCastException
这怎么会发生?我对此感到困惑。(因为它是通用的,所以我似乎应该做演员)
我找到了一些教程,他们是这样使用的:
table = new Entry[capacity];
()
甚至连演员都没有
我的代码如下
public class MyHashMap<K, V> {
private class MapEntry {
K key;
V value;
MapEntry next;
MapEntry(K key, V value) {
this.key = key;
this.value = value;
}
}
private int size = 0;
private int capacity;
MapEntry[] entry;
@SuppressWarnings("unchecked")
MyHashMap() {
capacity = 10;
entry = (MapEntry[]) new Object[capacity];
}
@SuppressWarnings("unchecked")
MyHashMap(int capacity) {
entry = (MapEntry[]) new Object[capacity];
}
public void put(K key, V value) {
int hash = hashCode(key);
MapEntry newNode = new MapEntry(key, value);
if (entry[hash % capacity] == null) {
entry[hash % capacity] = newNode;
} else {
if (key == entry[hash % capacity].key) {
entry[hash % capacity].value = value;
} else {
MapEntry nextNode = entry[hash % capacity].next;
while (nextNode != null) {
if (key == nextNode.key) {
nextNode.value = value;
return;
}
nextNode = nextNode.next;
}
nextNode = newNode;
}
}
}
public V get(K key) {
int hash = hashCode(key);
MapEntry node = entry[hash % capacity];
if (node == null) {
return null;
}
if (node.key == key) {
return node.value;
}
while (key != node.key) {
node = node.next;
if (node.key == key) {
return node.value;
}
}
return null;
}
public boolean contains(K key) {
return get(key) != null;
}
public int size() {
return size;
}
public void remove(K key) {
int hash = hashCode(key);
MapEntry node = entry[hash % capacity];
if (node == null) return;
if (key == node.key) {
entry[hash % capacity] = node.next;
}
MapEntry pre = node;
while (key != node.key) {
node = node.next;
if (key == node.key) {
pre.next = node.next;
return;
}
pre = pre.next;
}
}
private int hashCode(K key) {
return Math.abs(key.hashCode());
}
public void display(){
for(int i = 0; i < capacity; i++){
if(entry[i] != null){
MapEntry node = entry[i];
while(node != null){
System.out.print("{" + node.key + "=" + node.value + "}" + " ");
node = node.next;
}
}
}
}
public static void main(String[] args) {
MyHashMap<Integer, Integer> hashMapCustom = new MyHashMap<Integer, Integer>();
hashMapCustom.put(21, 12);
hashMapCustom.put(25, 121);
hashMapCustom.put(30, 151);
hashMapCustom.put(33, 15);
hashMapCustom.put(35, 89);
System.out.println("value corresponding to key 21="
+ hashMapCustom.get(21));
System.out.println("value corresponding to key 51="
+ hashMapCustom.get(51));
System.out.print("Displaying : ");
hashMapCustom.display();
System.out.print("Displaying : ");
hashMapCustom.display();
}
}
公共类MyHashMap{
私有类映射条目{
K键;
V值;
下一步是地图输入;
映射条目(K键,V值){
this.key=key;
这个值=值;
}
}
私有整数大小=0;
私人int能力;
地图条目[]条目;
@抑制警告(“未选中”)
MyHashMap(){
容量=10;
条目=(MapEntry[])新对象[容量];
}
@抑制警告(“未选中”)
MyHashMap(整数容量){
条目=(MapEntry[])新对象[容量];
}
公开作废认沽权(K键,V值){
int hash=hashCode(key);
MapEntry newNode=新的MapEntry(键,值);
if(条目[哈希%capacity]==null){
条目[哈希%capacity]=新节点;
}否则{
if(key==条目[hash%容量].key){
条目[hash%容量]。值=值;
}否则{
MapEntry nextNode=条目[hash%容量]。下一步;
while(nextNode!=null){
if(key==nextNode.key){
nextNode.value=值;
返回;
}
nextNode=nextNode.next;
}
nextNode=newNode;
}
}
}
公共V get(K键){
int hash=hashCode(key);
MapEntry节点=条目[hash%容量];
if(node==null){
返回null;
}
if(node.key==key){
返回node.value;
}
while(key!=node.key){
node=node.next;
if(node.key==key){
返回node.value;
}
}
返回null;
}
公共布尔包含(K键){
return get(key)!=null;
}
公共整数大小(){
返回大小;
}
公共无效删除(K键){
int hash=hashCode(key);
MapEntry节点=条目[hash%容量];
if(node==null)返回;
if(key==node.key){
条目[哈希%capacity]=node.next;
}
MapEntry pre=节点;
while(key!=node.key){
node=node.next;
if(key==node.key){
pre.next=node.next;
返回;
}
pre=pre.next;
}
}
私有整数哈希码(K密钥){
返回Math.abs(key.hashCode());
}
公共空间显示(){
对于(int i=0;i
您不能通过强制转换来转换数组的类,这就是您获得ClassCastException的原因。你应该使用
`Arrays.copyof ().`
CustomType[]ca=Arrays.copyOf(array,array.length,CustomType[].class);
我已经弄明白这是怎么回事了 (创建其组件类型为类型参数、具体参数化类型或有界通配符参数化类型的数组是不安全的。) 这样,就不会有错误 还有一个问题需要很好的解决。
为什么您不能直接执行
MapEntry[]entry=新建MapEntry[容量]
。之所以出现此错误,是因为并非所有对象
都属于MapEntry类
Cricket是正确的。例如,如果您尝试将字符串
强制转换为映射条目
,该怎么办。你认为它会工作吗?但如果我使用MapEntry[]entry=new MapEntry[capacity],它会说无法创建通用数组
entry = (MapEntry[]) Array.newInstance(MapEntry.class, capacity);