i have a private Node class with a getValue() method that returns the Node's value which is of generic type. In another class i've tried doing this:

T tempItem = null; ----also generic
tempItem = currentNode.getValue();

I get a complier error unless I cast it like this: tempItem = (T) currentNode.getValue();
Honestly i don't understand why i need to do this or if it actually works or not. Can someone explain?

Recommended Answers

All 9 Replies

What is getValue() defined to return?

Can you post a small complete program that generates the compiler error?

public class Node<T>{
    private Node <T> n;
    private T value;

    public Node(Node <T> x, T item){
        n = x;
        value = item;
    }

    public T getValue(){
        return value;
    }
}

Then in another class inside another method I wrote:
K tempItem = null;
tempItem = (K) currentNode.getValue();

Without the (K), it gives a complier error.

When you created the instances of Node that currentNode refers to, what did you have in the <>
How is curentNode defined?

... and what exactly is the compiler error!

private Node<K> currentNode = null;

if i write: tempItem = currentNode.getValue();
it generates incompatible types error.

Please post a COMPLETE program that shows the error message when you try to compile it.
Bits and pieces of code don't compile.

public class BinarySearchTree<T extends Comparable<? super T>> implements SearchTree<T> {

    private Node<T> root = null;
    private int size = 0;
    List<T> al = new ArrayList<T>();


public T getMax() {
        // TODO Auto-generated method stub
        Node tempNode = root.getRight();

        if(root == null){
            return null;
        }
        if(root.getRight() == null){//root is largest
            return (T) root.getValue();
        }
        else{//look in right subtree
            while(tempNode.getRight() != null){
                tempNode = tempNode.getRight();
            }
            return tempNode.getValue();//generates error
        }

    }
}

public class Node<K> {
        private K value;
        private Node<K> left;
        private Node<K> right;

        public Node(K value) {
            this(value, null, null);
        }

        public Node(K value, Node<K> left, Node<K> right) {
            this.value = key;
            this.left = left;
            this.right = right;
        }

        public K getValue() {
            return value;
        }
}

The rest of the code is long, but i think these are the relevant parts.

The posted code generates 10 errors when I try to compile it. Is that what you get?

One thing I notice is missing import statements, missing class definitions and missing methods.

Can you fix the code so it only generates the error you are asking about.

ok nevermind i figured out what was wrong. i forgot to put <T> when i created a new tempNode.

Node<T>tempNode = root;

No longer need to cast on tempNode.getValue()

thanks everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.