【问题标题】:ADT function keeps returning null and i have NO clue whyADT 函数一直返回 null,我不知道为什么
【发布时间】:2014-07-20 01:18:30
【问题描述】:

由于某种原因,我的插入函数不断返回 null 并打印出“未找到”。我觉得这与 head 为空且未分配为新节点这一事实有关。但我不能在 findKey 中执行此操作,因为它是一个私有类:

// findKey()
// returns a reference to the Node at key in the LinkedList; otherwise returns null
private Node findKey(String key){
    Node N = head;
 while(N != null){
    N = N.next;
    if(N.key.equals(key) && N.key != null){
      return N;
  }
    }
    return null;

}
public String lookup(String key){
 if(findKey(key) == null){
     System.out.println("not found");
     return null;
  }else{
  Node N = findKey(key);
  return N.value;
  }
}

public void insert(String key, String value)
  throws KeyCollisionException{

  if(lookup(key)!= null ){
     throw new KeyCollisionException(
        "cannot create duplicate key");
  }
  if(head == null){
    head = new Node(key,value);
    return;
    }else{
    Node iter = head;
    while(iter.next != null){
    iter = iter.next;
    }
    Node N = new Node(key,value);
    iter.next = N;
    numItems++;
    }

}

【问题讨论】:

  • 你不能在insert() 中添加一个检查来查看head 是否为空吗?如果是那么只是自动添加当前键/值?
  • 我认为这就是我试图在行中做的:if(head == null){ head = new Node(key,value);返回;
  • 你做了,但只是 在 调用 lookup() 然后调用 findKey() 然后使用 head(可能为 null)作为 N 的值。
  • 哦,好吧。我在调用 lookup() 之前将支票移到了
  • 这对你有用吗?如果是这样,我会将其发布为答案。

标签: java linked-list nodes


【解决方案1】:

您在findKey 中过早地移动到下一个节点,这意味着您永远不会检查head 节点以查看它是否包含所需的密钥,并且如果未找到该密钥,您将获得 NullPointerExceptions。

只需将N = N.next; 行向下移动一点。此外,在if 语句中交换检查顺序(尽管您的Node 构造函数无论如何都应该防止空键):

private Node findKey(String key){
    Node N = head;

    while(N != null){
        if(N.key != null && N.key.equals(key)){
            return N;
        }
        N = N.next;
    }
    return null;
}

【讨论】:

  • 我试过了,但它仍然打印出“未找到”:(
【解决方案2】:

您应该在构造函数中使用 this 关键字:

Node(String key, String value){
    while(key != null && value !=null){
    this.key = key;
    this.value = value;
    next = null;
}

而且“while”不是必需的。使用“如果”更好。

【讨论】:

  • 这很有效!现在它打印出来: not found not found 。 .未找到 1 2a 3b 4c 5d 6e 7f 8g
  • 代码中的另一个问题是在 findKey() 内部,永远不会检查 head。您应该在比较密钥后执行“N = N.next”,如下所示: while(N != null){ if(N.key.equals(key) && N.key != null){ return N; } N = N.下一个; }
【解决方案3】:

有几个问题可能会导致您的问题。首先,因为您对类的字段和构造函数的参数使用相同的变量名,所以您应该使用 this 关键字来区分这 2 个。此外,您应该将 while 循环更改为 if 语句,因为您不是循环遍历任何东西。如果 if 语句失败,您应该设置一些默认值或抛出 NullPointerException。变化:

String key;
String value;

Node(String key, String value) {
    while(key != null && value != null) {
        key = key;
        value = value;
    }
}

到

String key;
String value;

Node(String key, String value) {
    if(key != null && value != null) {
        this.key = key;
        this.value = value;
    }
    else {
        throw new NullPointerException("Initial values cannot be null");
    }
}

其次,你有

if(lookup(key) != null ) {
    throw new KeyCollisionException("cannot create duplicate key");
}
if(head == null) {
    head = new Node(key,value);
    ...
}

如果head 为空,最好将它设置为一个新节点,但是在进行方法调用后会导致head 在@ 内部被引用987654328@。您应该切换这些操作的顺序以防止您的NullPointerException。

if(head == null) {
    head = new Node(key,value);
    ...
}
if(lookup(key) != null ) {
    throw new KeyCollisionException("cannot create duplicate key");
}

【讨论】:

    猜你喜欢
    • 2017-06-21
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多