我需要做的是,计算两个客户的评级之间的点积距离。客户的评级记录在散列图中。
private HashMap<String,int[]> ratingmap;
hashmap中的键是客户名称,与之关联的是该客户的评级(他对书籍的评分)
我将如何做到这一点?
/**
* calculate dot product distance between the ratings of two customers
* @param name1 String name of customer
* @param name2 String name of customer
* @return int distance or ILLEGAL_INPUT if name1 or name2 are not customers
*/
public int distance(String name1, String name2)
{
return 0; //replace with code
}
以下是RatingsAnalysis
类
//collection of rated book titles
private BookList books;
//collection of customers and their ratings
private RatingsMap ratings;
//use a list of customer names from the ratings map for looping through the map
private ArrayList<String> customernames;
答案 0 :(得分:0)
据推测,成员字段
private RatingsMap ratings;
RatingsAnalysis
中的是Map
名称=&gt;收视率。 distance
方法中的任务是:
name1
和name2
的评分,并将其存储在本地变量中(提示:使用get
上定义的Map
方法} interface)Rating
提供有关其含义的更多信息。作为旁注,RatingsMap可以而且应该更强类型化:
private RatingsMap<String, Rating> ratings;
编辑:添加请求的骨架代码......
public int distance(String name1, String name2)
{
Rating r1 = this.ratings.get(name1);
Rating r2 = this.ratings.get(name2);
if(r1 != null && r2 != null) {
return ....
}
}