【发布时间】:2015-04-28 05:06:55
【问题描述】:
您好,我是 Java 新手,在linkedList 上运行 2 种方法时遇到了这个问题。
我写的 find 函数总是返回 false。 find 方法是将类型 E 元素作为参数,如果项目在链表中则返回 true,否则返回 false。
max 方法是如果列表不为空,则返回列表中的最大元素(本例中为最长字符串),如果列表为空,则返回 null。比较必须通过 compareTo() 来完成。 我写的最大值只查看每个元素(字符串)的第一个字母。
非常感谢任何帮助!
public boolean find(E e){
LinkedListTest<E>.Node node = null;
Node current =node;
while (current != null){
if (current.equals(e)){
return true;
}
else{
current=current.next;
}
}
return false;
}
public E max(){
Iterator<E> iterator=iterator();
E max = iterator.next();
while (iterator.hasNext())
{
E next = iterator.next();
if (max.compareTo(next) > 0)
max = next;
}
return max;
}
【问题讨论】:
标签: java linked-list equals compareto