gpt4 book ai didi

java - 谁能告诉我为什么下面的代码会生成空指针异常?

转载 作者:行者123 更新时间:2023-12-01 11:42:22 27 4
gpt4 key购买 nike

我正在开发一个项目,我必须编写一个类SeparateChainingMap,它代表一个单独的链接哈希表并实现Map 接口(interface)。每次运行代码时,我都会收到由插入方法引起的空指针异常。谁能指导我做错了什么?

我已经在下面包含了到目前为止的所有代码。当我调用插入方法时抛出 NullPointerException。

import java.util.LinkedList;
import java.util.*;


public class SeparateChainingMap<K extends Comparable<? super K>, V> implements Map<K,V> {

public static final int INITIAL_SIZE = 10;
private int currentSize;
private LinkedList<Pair<K,V>>[] table;


@SuppressWarnings("unchecked")
public SeparateChainingMap() {
table = (LinkedList<Pair<K,V>>[]) new LinkedList[INITIAL_SIZE];
}

@SuppressWarnings("unchecked")
public SeparateChainingMap (int size) {
LinkedList<Pair<K,V>> [] table = new LinkedList[INITIAL_SIZE];
for (int i=0; i<table.length; i++) {
table[i] = new LinkedList<Pair<K,V>>();
}
}

//Introducing the hash function
private int myHash(Pair<K,V> x) {
int hashValue = x.hashCode();
//divide the hashValue by mod table length
hashValue %= table.length;

if(hashValue<0) {
hashValue += table.length;
}
System.out.println(hashValue);
return hashValue;
}

//rehash function
@SuppressWarnings("unchecked")
private void rehash() {
LinkedList<Pair<K,V>>[] oldTable = table;

//Create new double-sized empty tables
table = (LinkedList<Pair<K,V>>[]) new LinkedList[2 * table.length];

for (int j=0; j< table.length; j++) {
table[j] = new LinkedList<>();
}

//Copy table over
currentSize = 0;
for (int i=0; i<oldTable.length; i++) {
for(int j=0; j<oldTable[i].size(); j++){
insert(oldTable[i].get(j));

}

}

}

//Find an item in the hash table. returns true if x is found
public boolean contains(Pair<K,V> x) {
LinkedList<Pair<K,V>> thisList = table[myHash(x)];
return thisList.contains(x);
}

//Method for inserting a pair into the hash table
public void insert(Pair<K,V> x) {
LinkedList<Pair<K,V>> thisList = table[myHash(x)];

if(!thisList.contains(x))
{
thisList.add(x);
}

if (++currentSize > table.length) {
rehash();
}


}

public void remove (Pair<K,V> x) {
LinkedList<Pair<K,V>> thisList = table[myHash(x)];

if(thisList.contains(x)) {
thisList.remove(x);
currentSize--;
}
}

public void makeEmpty()
{
for(int i = 0; i<table.length; i++) {
table[i].clear();
currentSize = 0;
}
}

//map functions
@SuppressWarnings("unchecked")
public V get (K key) {
Pair<K,V> pair = new Pair<K,V>(key,null);
int i = myHash(pair);

for(int j=0; j<table.length; j++) {

if(i == j) {
for(int k=0; k<table[j].size(); k++)
{
if(key.equals(table[j].get(k).key)){
V value = table[j].get(k).value;
return value;
}
}


}

}


return null;
}

public void put(K key, V value) {
Pair<K,V> pair = new Pair<K,V>(key, value);
insert(pair);

}


public static void main(String args[]) {
@SuppressWarnings("unchecked")

SeparateChainingMap<String,Integer> SCM = new SeparateChainingMap<>();

SCM.put("Alice", 243);

}

最佳答案

@SuppressWarnings("unchecked")
public SeparateChainingMap() {
table = (LinkedList<Pair<K,V>>[]) new LinkedList[INITIAL_SIZE];
}

此构造函数的 table[i] 未启动。它应该与:

@SuppressWarnings("unchecked")
public SeparateChainingMap (int size) {
LinkedList<Pair<K,V>> [] table = new LinkedList[INITIAL_SIZE];
for (int i=0; i<table.length; i++) {
table[i] = new LinkedList<Pair<K,V>>();
}
}

关于java - 谁能告诉我为什么下面的代码会生成空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29425137/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com