二元运算符 '-' 的错误操作数类型有节点
我正在尝试创建一个带有双向链表节点的优先级队列来对双向链表进行排序。 https://www.codingninjas.com/codestudio/problems/sort-ak-sorted-doubly-linked-list_1118118?leftPanelTab=0
/****************************************************************
Following is the class structure of the Node class:
class Node<T> {
T data;
Node<T> next;
Node<T> prev;
public Node(T data) {
this.data = data;
}
}
*****************************************************************/
import java.util.*;
public class Solution {
static class compareNode implements Comparator<Node>
{
public int compare(Node n1, Node n2){
return n1.data-n2.data;
}
}
public static Node<Integer> sortedDll(Node<Integer> head, int k) {
PriorityQueue<Node> pq = new PriorityQueue<Node>(new compareNode());
/* logic goes here*/
return head;
}
}
当我运行上述程序时,我遇到以下错误,
Compilation Failed
./Solution.java:21: error: bad operand types for binary operator '-'
return n1.data-n2.data;
^
first type: Object
second type: Object
1 error
谁能告诉我为什么我会收到此错误?以及如何纠正它?
I am trying to create a Priority Queue with Nodes of doubly Linked List to sort the doubly linked list .
https://www.codingninjas.com/codestudio/problems/sort-a-k-sorted-doubly-linked-list_1118118?leftPanelTab=0
/****************************************************************
Following is the class structure of the Node class:
class Node<T> {
T data;
Node<T> next;
Node<T> prev;
public Node(T data) {
this.data = data;
}
}
*****************************************************************/
import java.util.*;
public class Solution {
static class compareNode implements Comparator<Node>
{
public int compare(Node n1, Node n2){
return n1.data-n2.data;
}
}
public static Node<Integer> sortedDll(Node<Integer> head, int k) {
PriorityQueue<Node> pq = new PriorityQueue<Node>(new compareNode());
/* logic goes here*/
return head;
}
}
i am getting below error when i run the above program
Compilation Failed
./Solution.java:21: error: bad operand types for binary operator '-'
return n1.data-n2.data;
^
first type: Object
second type: Object
1 error
can anyone tell why i am getting this error? and how to rectify it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您无法从另一个对象中减去任意对象。
您实际上在这里尝试做的是从另一个
T
中减去一个T
。一般来说,这不是一件有效的事情。 (T
的擦除是Object
。)这意味着这
是行不通的。
但是...等等...这个问题实际上需要您的代码比较
Node
实例而不是Node
实例。所以你需要的是有点像这样的东西:...我想你会发现它可以编译。但这是不正确的。正确的写法是:
问题是,当你从一个整数中减去另一个整数时,你可能得到整数溢出......并且结果符号错误。
解决此问题的另一种方法是尝试将
Node
的data
值转换为Comparable
,然后使用可比较.比较
。这种方法将涉及一些隐式和显式的运行时类型检查......并且理论上可能会产生 ClassCastException 。You cannot subtract an arbitrary object from another one
What you are actually trying to do here is to subtract one
T
from anotherT
. That is not a valid thing to do in general. (The erasure ofT
isObject
.)That means that
is not going to work.
But ... wait ... the problem actually requires your code to compare
Node<Integer>
instances rather thanNode<?>
instances. So what you need is something a bit like this:... and I think you will find that that compiles. But it is not correct. The correct way to write that is:
The problem is that when you subtract one integer from another you can get integer overflow ... and a result with the wrong sign.
Another way to solve this is attempt cast the
data
values of aNode<T>
toComparable<?>
and then useComparable.compare
. This approach would involve some implicit and explicit runtime type checks ... and could in theory produceClassCastException
s.