问题描述:

How can i compare 2 Tree nodes of type Integer in my data structure? If i use == it gives NullPointerException error for values greater than 128...

public int brElNaPutanji(BNode<Integer> tekuci, BNode<Integer> kraj){

if (tekuci == kraj)

return 0;

int trt = kraj.info;

int prt = tekuci.info;

if (trt < prt)

return 1 + brElNaPutanji(tekuci.left, kraj);

else

return 1 + brElNaPutanji(tekuci.right, kraj);

}

And here is the BNode class

class BNode<E extends Comparable<E>> {

public E info;

public BNode<E> left;

public BNode<E> right;

public BNode(E info) {

this.info = info;

left = null;

right = null;

}

public BNode(E info, BNode<E> left, BNode<E> right) {

this.info = info;

this.left = left;

this.right = right;

}

}

网友答案:

Use number1.equals(number2) instead of == as == only checks if they are in the same memory area ,may not always be correct .

相关阅读:
Top