hi i'm new java , have problem linked list. find , max methods not producing correct output. find method take type e element argument, , returns true if item in linked list, or false otherwise. max method return maximum element in list if list not empty, or null if empty list. comparison has done compareto().
i have tried finding "apple", indeed in list, however, return false. max element got "apple", instead of "watermelon".
any appreciated!
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; }
if (current.equals(e)){ return true;
you need compare node's item, not node itself.
if (max.compareto(next) > 0) max = next;
that comparison needs reversed: have found new max
if old 1 smaller current item.