gpt4 book ai didi

java - eclipse IDE错误警告未使用的字段

转载 作者:行者123 更新时间:2023-12-02 01:31:14 26 4
gpt4 key购买 nike

我正在为学习算法编写二叉搜索树。我使用 eclipse 作为我的 IDE。我的编辑器窗口中有一个警告,但我认为没有任何不正确的定义或使用。警告是 The value of the field BST<Key,Value>.Node.value is not used 。但是你可以从我的小程序中看到,值字段肯定在构造函数中使用。我多次保存并编译。但它保留在那里。我是一个追求完美的人。所以我可以理解这个警告,因为我没有按照它提醒的那样做错。因此,我将小程序粘贴到此处,希望有人可以观看它并告诉我是否弄错了。

package com.raymei.search;

public class BST <Key extends Comparable<Key>, Value> {

private class Node {
private Key key;
private Value value; // warning position
private Node left;
private Node right;

public Node(Key key, Value value) {
this.key = key;
this.value = value;
this.left = null;
this.right = null;
}
}

private Node root;
private int count;

public BST() {
root = null;
count = 0;
}

public int size() { return count; }

public boolean isEmpty() { return count == 0; }

public void insert(Key key, Value value) {
root = insert(root, key, value);
}

public Node insert(Node node, Key key, Value value) {

if (node == null) {
node = new Node(key, value);
count++;
return node;
}

if (key.equals(node.key)) {
node.value = value;
} else if (key.compareTo(node.key) < 0) {
node.left = insert(node.left, key, value);
} else {
node.right = insert(node.right, key, value);
}

return node;
}

public static void main(String[] args) {
BST<String, Integer> bst = new BST<>();
bst.insert("Tom", 19);
bst.insert("Kate", 20);
bst.insert("Leonard", 20);
bst.insert("Hulk", 35);
System.out.println(bst.count);
}

}

最佳答案

虽然您确实在构造函数中设置了 value 字段,但它实际上并未在 Node 类中的任何位置使用。设置字段和使用字段之间存在差异。如果您从代码中省略 value 变量,则不会产生任何差异。这就是警告消息所指示的内容。

关于java - eclipse IDE错误警告未使用的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56019764/

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