以下是我的JAVA方法
public static List<Match<String, String>> Decider
(List<Preference<String, String>> hospitalPrefs,List<Preference<String, String>> studentPrefs)
{
Match<String, String> matching = new Match<String, String>(null, null);
List<Match<String, String>> matcher = new ArrayList<Match<String, String>>();
/** Matching the preference of the hospital with the student */
for(int hospitalLoop = 0;hospitalLoop < hospitalPrefs.size(); hospitalLoop++)
{
String hospitalPreferrer = hospitalPrefs.get(hospitalLoop).getPreferrer();
String hospitalPreferred = hospitalPrefs.get(hospitalLoop).getPreferred();
int hospitalValue = hospitalPrefs.get(hospitalLoop).getValue();
for(int studentLoop = 0;studentLoop < studentPrefs.size();studentLoop++)
{
String studentPreferrer = studentPrefs.get(studentLoop).getPreferrer();
String studentPreferred = studentPrefs.get(studentLoop).getPreferred();
int studentValue = studentPrefs.get(studentLoop).getValue();
if(hospitalPreferred.equals(studentPreferrer)
&& hospitalPreferrer.equals(studentPreferred)
&& hospitalValue == studentValue)
{
System.out.println(hospitalPreferred + "," + studentPreferred);
matching.setItem1(hospitalPreferred);
matching.setItem2(studentPreferred);
matcher.add(matching);
break;
}
}
}
return matcher;
}
matcher变量覆盖列表。我很困惑。
如果我要添加左侧的话 A,B,C。
在matcher变量中,它正在添加 C,C,C
我在出错的地方感到困惑。
谢谢!!!
答案 0 :(得分:0)
初始化匹配一次,然后更改其值。你需要
matching = new Match<String,String>();
在你的循环中的某个地方。
答案 1 :(得分:0)
您正在循环之前创建matching
的实例,然后将相同的实例添加到您的集合中。你可能想在循环中创建匹配:
................
System.out.println(hospitalPreferred + "," + studentPreferred);
Match<String, String> matching = new Match<String, String>(null, null);
matching.setItem1(hospitalPreferred);
matching.setItem2(studentPreferred);
matcher.add(matching);
break;
................
答案 2 :(得分:0)
您只是重复添加相同的matching
。
如果我是你,我会搬家
Match<String, String> matching = new Match<String, String>(null, null);
之前
matching.setItem1(hospitalPreferred);
matching.setItem2(studentPreferred);
matcher.add(matching);