Tell a Java Class<T> that T has a certain method in it -


i implementing algorithm generate high variance clusters in dataset. throughout different tests, using many different data types, need make cluster class usable data type t. so, have:

public class cluster<t> {     private int score;     private arraylist<t> list;     private int k; 

and able assess effectiveness of algorithm, have score computed based on distance between each object in cluster. here computescore() method:

private void computescore() {     if (list.size() < 2) score = 0;     else if (list.size() == 2) {         t x = list.remove(1);         t y = list.remove(0);         score = x.distanceto(y);         list.add(y);         list.add(x);     } } 

the problem in computescore() method, line score = x.distanceto(y) throws error because type t, distanceto() method undefined. of datatypes, defining method use here. how tell cluster class, distanceto() defined?

thanks in advance help.

i guess use <t extends yourclass> , define distanceto() in yourclass.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -