0

我需要做的是,计算两个客户评分之间的点积距离。客户的评分记录在哈希图中。

private HashMap<String,int[]> ratingmap;

哈希图中的键是客户名称,与之相关的是该客户的评分(他对书籍的评分)

我将如何做到这一点?

/**
 * 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;
4

1 回答 1

0

想必是成员字段

private RatingsMap ratings;

inRatingsAnalysisMap名称 => 评级。您在该distance方法中的任务是:

  1. 查找评分name1并将name2它们存储在局部变量中(提示:使用接口get上定义的方法Map
  2. 对上面获得的这两个评级执行点积分析。您要么知道如何执行此操作,要么需要提供有关Rating此问题中 a 含义的更多信息。
  3. 返回结果

附带说明一下,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 ....
    }
}
于 2012-05-24T11:31:45.080 回答