java - Array object and maximum -


i know how find minimum , maximum in array. if method lets called fmax():

public static double fmax(object[] stuff) 

the parameter array object how go finding max of array? cannot do. okay how if want method return double , if memory hasnt been allocated parameter named stuff return value negative_infinity in double class, otherwise return value maximum value elements in stuff array

object max = stuff[0]; (int = 0; < stuff.length; i++) {     if (data[i] > max) {         max = stuff[i];     } } 

to find maximum of something, either a) needs implement comparable interface b) need have sort of explicit criteria determining maximum is, can put in instance of comparator

object isn't going have useful sorting. if subclass object, sort based on components of object.

public class example implements comparable {     int sortablevalue = 0;      public example (int value)     {         this.sortablevalue = value;     }      public int compareto(example other)     {         return integer.compare(this.sortablevalue, other.sortablevalue);     } } 

that's object definition has natural sorting order. java can @ of built in sorting algorithms , know order belong in.

if don't provide java means of determining how object has greater or lesser relative value compared object of same type, won't figure out on own.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -