getMax()操作符” < "不能应用于

huangapple go评论62阅读模式
英文:

getMax() operater "<" cannot be applied to <T>

问题

我对链表和节点非常陌生,所以在Java中的getMax()方法上完全卡住了。

所以我正在尝试获取列表中的最大值,但是我不能在if语句中使用"<",我应该用什么代替呢?我需要使用compareTo吗?但是我不知道我应该比较什么。

到目前为止,这是我拥有的代码:

public T getMax()
{
    T largestValue = null;
    Node<T> currNode = this.firstNode;
    while (currNode.next != null)
    {
        if(largestValue < currNode.next.data)


    }
}
英文:

I am very new to linked lists and nodes so I am completely stuck on my getMax() method in Java.

So I am trying to get the largest value in the list but I can't use "<" in my if statment, what am I supposed to use instead? Do I need to do compareTo? But then I don't know what I would be comparing.

This is the code I have so far:

public T getMax()
    {
        T largestValue = null;
        Node&lt;T&gt; currNode = this.firstNode;
        while (currNode.next != null)
        {
            if(largestValue &lt; currNode.next.data)


        }

答案1

得分: 4

你(可能;你的片段太短)对 T 没有任何限制,因此 T 可以是任何东西。值得注意的是,它可能是例如 InputStream,它本身不具备可比性。因此,无法对此应用任何形式的“这个 T 的特定实例是否在另一个 T 的实例之下”的操作,因为甚至无法保证这个操作有意义。

你需要让这变得有意义。

在 Java 中,这将是 Comparable 的工作,或者,你可以将“我要操作的对象类型”和“能够确定给定两个对象中哪个较低”的代码分开。

前者意味着你希望 T 被限制。你也在处理 next 方面出现了问题。我会修复这两个问题。

public class MyClassThingie<T extends Comparable<T>> {
    public T getMax() {
        T largestValue = null;
        Node<T> currNode = this.firstNode;
        while (currNode != null) {
            if (largestValue == null || largestValue.compareTo(currNode.data) < 0) largestValue = currNode.data;
            currNode = currNode.next;
        }
        return largestValue;
        // 如果没有元素,你可能希望指定方法返回 null。
    }
}

如果你想要另一种模型,可以拥有一个接受 Comparator<T> 的构造函数;可以参考例如 TreeSet 的源代码获得一些示例。

注意,T extends Comparable<T> 的意思是:T 是一些类型,满足以下条件:它实现了 Comparable<T>。换句话说,它是一种能够将自身与同类其他对象进行比较的东西。String 就是这样的类型。Integer 也是(Integer 实现了 Comparable<Integer>;String 实现了 Comparable<String>)。

英文:

You (presumably; your snippet is too short) have no bounds on T, so T could be anything. Notably, it could be, say, InputStream, which is not inherently comparable. Therefore, it is not possible to apply any sort of "is this particular instance of T 'below' this other instance of T" operation on this, as there is no guarantee this operation even makes sense.

You need to make that make sense.

In java, that'll be the job of Comparable, or, you separate out the notion of 'the kind of thing I operate on' and 'code that can determine, given 2 things, which one is lower'.

The former means you want T to be bound. You also messed up your handling of next. I'll fix both.

public class MyClassThingie&lt;T extends Comparable&lt;T&gt;&gt; {
    public T getMax() {
        T largestValue = null;
        Node&lt;T&gt; currNode = this.firstNode;
        while (currNode != null) {
            if (largestValue == null || largestValue.compareTo(currNode.data) &lt; 0) largestValue = currNode.data;
            currNode = currNode.next;
        }
        return largestValue;
        // you may want to spec out that your method returns null if there are 0 elements.
    }
}

If you want the other model, have a constructor that accepts a Comparator&lt;T&gt;; see the source of e.g. TreeSet for some examples.

Note that T extends Comparable&lt;T&gt; means: T is some type for which the following holds: It implements Comparable&lt;T&gt;. In other words, it is a thing that is capable of comparing itself to other things of its own kind. String is such a type. So is Integer. (Integer implements Comparable&lt;Integer&gt;; String implements Comparable&lt;String&gt;).

huangapple
  • 本文由 发表于 2020年9月15日 02:10:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63889689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定