class Node<T extends Number> implements Comparable<Node<T>>{
private T data;
private Node<T> next;
private Node<T> prev;
public Node(T data){
this.data = data;
next = null;
prev = null;
}
public int compareTo(Node<T> o){
if(o.data > this.data){
return -1;
}else if(o.data < this.data){
return 1;
}else{
return 0;
}
}
I wrote the above code and when I try to compile it I get
Queue.java:14: error: bad operand types for binary operator '>'
if(o.data > this.data){
^
first type: T
second type: T
where T is a type-variable:
T extends Number declared in class Queue.Node
Queue.java:16: error: bad operand types for binary operator '<'
}else if(o.data < this.data){
^
first type: T
second type: T
where T is a type-variable:
T extends Number declared in class Queue.Node
2 errors
just so you don't get confused by the Queue.node, Class Node is embedded in Class Queue
So I'm guessing that Java does not auto unbox Number but is there a way to do it?
Thanks
You can use doubleValue()
:
class Node<T extends Number> implements Comparable<Node<T>>
{
private T data;
private Node<T> next;
private Node<T> prev;
public Node(T data)
{
this.data = data;
next = null;
prev = null;
}
public int compareTo(Node<T> o)
{
if(o.data.doubleValue() > this.data.doubleValue()) return -1;
else if(o.data.doubleValue() < this.data.doubleValue()) return 1;
else return 0;
}
}
The Number
class is abstract and cannot be used the same was as the classes that extend it.
For instance:
// this doesnt work
Number a = 22;
Number b = 33;
Number c = a - b; // compile error
if(a > b) // compile error
You need to compare some value of these Number objects.
// this works
Number a = 22;
Number b = 33;
Number c = a.doubleValue() - b.doubleValue();
if(a.longValue() > b.longValue())
So to fix your code your statement should read if(o.data.doubleValue() > this.data.longValue())
So I'm guessing that Java does not auto unbox Number
That is correct. Unboxing is only possible when the static type of the expression you are attempting to use is one of the primitive wrapper types; eg
Number n = ...
int i = 1 + n; // Compilation error
int j = 1 + ((Integer) n); // OK ... provided the cast is going to work.
but is there a way to do it?
If you know that the Number
is a specific type, then you can cast it as above. Otherwise, you can call the respective ...Value()
method explicitly.
int j = 1 + n.intValue();
However, there is no way that you can make the operators ('+', '>' etcetera) behave as if they were overloaded for the Number
class.
不要理会T
作为Number
:使T extend Comparable<T>
,并将其实现为return this.data.compareTo(o.data)
。
You can use the intValue() method on node Type which will return you integer value. Similar methods are also available for other data types (doubleValue(), longValue() etc.).
Following will be the code snippet look like:
public int compareTo(Node<T> o){
if(o.data.intValue() > this.data.intValue()){
return -1;
}else if(o.data.intValue() < this.data.intValue()){
return 1;
}else{
return 0;
}
}
You should use the intValue()
or method (or any of its other versions, if you want eg long
or double
. Also works if it isn't an Integer
.
public int compareTo(Node<T> o){
if(o.data.intValue() > this.data.intValue()){
return -1;
}else if(o.data.intValue() < this.data.intValue()){
return 1;
}else{
return 0;
}
}
Although what you ask is not possible in general, in this case you can use the fact that all subclasses of Number also implement Comparable. If you add & Comparable to the definition of T you can simply compare the values.
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.