Java programmeerimise konspekt
compareTo ((Comparable)a.get (i)) < 0)
maks = (Comparable)a.get (i);
}
return maks;
} // maksimum lopp
Näide. Veel üks maksimumi leidmine
/**
* Maksimaalse elemendi leidmine.
* @param a Collection, mille
* elemendid rahuldavad liidest Comparable.
* @return maksimaalne element.
* @throws NoSuchElementException kui a
on tühi.
*/
static public Comparable maksimum2 (Collection a) {
Comparable maks;
Iterator iteraator = a.iterator();
if (iteraator.hasNext())
maks = (Comparable)iteraator.next();
else throw new NoSuchElementException ("
maksimumi leidmisel");
Comparable c;
while (iteraator.hasNext()) {
c = (Comparable)iteraator.next();
if (maks.compareTo (c)<0)
maks = c;
}
return maks;