I have a question: I work in an Eclipse environment.
Sometimes the computer does not give the following casting:
int a ...
Object ans = (int) a;
But only this conversion:
int a ...
Object ans = (Integer) a;
I understand why you can do between casting Objectin Integer, but why primitive variable - there are times when you can, and have the time, you can not do the casting?
thank
I am attaching code that the compiler does not allow me to cast between a intvariable in an object:
public Object minimum(){
return minimum(this.root);
}
public Object minimum(BSTNode node){
if (node.left != null) return minimum(node.left);
return node.data;
}
public Object maximum(){
return maximum(this.root);
}
public Object maximum(BSTNode node){
if (node.right != null) return maximum(node.right);
return node.data;
}
public Object findNearestSmall(Object elem) {
int diff;
diff = (int)maximum() - (int)minimum();
if (compare(minimum(), elem) == 0) return elem;
else return findNearestSmall(elem, this.root, diff);
}
public Object findNearestSmall(Object elem, BSTNode node, int mindiff){
if(node == null) return (int)elem - mindiff;
int diff = (int)elem - (int)node.data;
if(diff > 0 && mindiff > diff) mindiff = diff;
if(compare(node.data, elem)>-1)
return findNearestSmall(elem, node.left, mindiff);
else
return findNearestSmall(elem, node.right, mindiff);
}
source
share