简体   繁体   中英

eclipse IDE wrong warning that unused field

I am writing binary search tree for learning algorithm. I used eclipse as my IDE. There is a warning in my editor window, but I think there isn't any non-proper definition or using. The warning is The value of the field BST<Key,Value>.Node.value is not used . But you can see from my applet, that the filed of value is used in constructor definitely. I save it for many times and compile it. But it keep there. I am a man pursuing perfect. So I can understand this warning, because I don't make it wrong as it remind. So I paste the applet here and want somebody could watch it and tell me whether I have make it wrong.

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);
    }

}

While you do set the value field in your constructor, it isn't actually used anywhere in the Node class. There is a difference between setting a field and using it. If you would omit the value variable from your code, it would not make a single difference. So that's what the warning message indicates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM