We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,901 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

do i need to cast a generic type

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?

3
Contributors
9
Replies
1 Day
Discussion Span
8 Months Ago
Last Updated
10
Views
Question
Answered
plasticfood
Junior Poster
112 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What is getValue() defined to return?

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
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.

plasticfood
Junior Poster
112 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,514 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

... and what exactly is the compiler error!

JamesCherrill
... trying to help
Moderator
8,514 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

private Node<K> currentNode = null;

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

plasticfood
Junior Poster
112 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
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.

plasticfood
Junior Poster
112 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

plasticfood
Junior Poster
112 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 8 Months Ago by NormR1 and JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1322 seconds using 2.74MB