java - In convertible cast, casting from T to double -
i want create generic method getting maximal value array. got stuck @ problem of conversion between t type math.max method uses.
here code: first cannot initialize maxvalue like:
t maxvaue = 0;
secondly cannot use data[i] in math.max, in both cases got error of in convertible cast.
class matrix<t>{ public t getmaxvalue(t[] data){ t maxvalue; (int i=0; i<data.length; i++){ maxvalue = math.max(data[i], maxvalue); } return maxvalue; } }
math.max takes int, float, double , long. can not use t
.
what can write own max() method using t. example, method take comparator
object in param. or t objects can implement comparable
interface.
have @ post : how implement generic `max(comparable a, comparable b)` function in java?
Comments
Post a Comment