问题描述
我对链表和节点非常陌生,因此我完全陷入了Java的getMax()方法中。
因此,我试图在列表中获得最大的价值,但我不能在if语句中使用“
这是我到目前为止的代码:
public T getMax()
{
T largestValue = null;
Node<T> currNode = this.firstNode;
while (currNode.next != null)
{
if(largestValue < currNode.next.data)
}
解决方法
您(大概;您的代码段太短)对T没有限制,所以T可以是任何值。值得注意的是,它可能是DF_out <- data.frame(Date = c("1979-01-01","1979-01-01"),Variables = c("Grid_2","Grid_20"),Values = c(0.95,1.3),Station_no = c(2,20))
,这并不是固有的可比性。因此,不可能对此应用任何类型的“ T的特定实例在此T的其他实例下方”吗,因为无法保证该操作甚至是有意义的。 / p>
您需要使之有意义。
在Java中,这将是InputStream
,或的工作,您将“我从事的事情的种类”和“可以确定,给出两件事,哪一个更低。
前者意味着您希望绑定T。您还弄乱了对Comparable
的处理。我会修复的。
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;
// you may want to spec out that your method returns null if there are 0 elements.
}
}
的构造函数;参见例如Comparator<T>
为例。
请注意,TreeSet
的含义是:T是某种类型,其适用于以下情况:它实现T extends Comparable<T>
。换句话说,这是一种能够与其他同类事物进行比较的事物。 Comparable<T>
就是这种类型。 String
也是如此。 (整数实现Integer
;字符串实现Comparable<Integer>
)。