二元运算符 '-' 的错误操作数类型有节点

发布于 01-15 02:19 字数 1416 浏览 2 评论 0原文

我正在尝试创建一个带有双向链表节点的优先级队列来对双向链表进行排序。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove2025-01-22 02:19:05

您无法从另一个对象中减去任意对象。

您实际上在这里尝试做的是从另一个 T 中减去一个 T。一般来说,这不是一件有效的事情。 (T 的擦除是Object。)

这意味着这

static class compareNode implements Comparator<Node> {
    public int compare(Node n1, Node n2){
        return n1.data-n2.data;
    }
}

是行不通的。

但是...等等...这个问题实际上需要您的代码比较 Node 实例而不是 Node 实例。所以你需要的是有点像这样的东西:

static class compareNode implements Comparator<Node<Integer>> {
    public int compare(Node<Integer> n1, Node<Integer> n2){
        return n1.data-n2.data;
    }
}

...我想你会发现它可以编译。但这是不正确的。正确的写法是:

static class compareNode implements Comparator<Node<Integer>> {
    public int compare(Node<Integer> n1, Node<Integer> n2){
        return n1.data.compareTo(n2.data);
    }
}

问题是,当你从一个整数中减去另一个整数时,你可能得到整数溢出......并且结果符号错误。


解决此问题的另一种方法是尝试将 Nodedata 值转换为 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 another T. That is not a valid thing to do in general. (The erasure of T is Object.)

That means that

static class compareNode implements Comparator<Node> {
    public int compare(Node n1, Node n2){
        return n1.data-n2.data;
    }
}

is not going to work.

But ... wait ... the problem actually requires your code to compare Node<Integer> instances rather than Node<?> instances. So what you need is something a bit like this:

static class compareNode implements Comparator<Node<Integer>> {
    public int compare(Node<Integer> n1, Node<Integer> n2){
        return n1.data-n2.data;
    }
}

... and I think you will find that that compiles. But it is not correct. The correct way to write that is:

static class compareNode implements Comparator<Node<Integer>> {
    public int compare(Node<Integer> n1, Node<Integer> n2){
        return n1.data.compareTo(n2.data);
    }
}

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 a Node<T> to Comparable<?> and then use Comparable.compare. This approach would involve some implicit and explicit runtime type checks ... and could in theory produce ClassCastExceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文