2013-04-09 60 views
-1

下面是我的Java方法列表中添加一次overwrting又一次

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; 
} 

匹配变量是覆盖列表。我对此感到困惑。

有点像我加入
a,b,c。

在匹配变量是加 C,C,C

我很困惑,我要去错了。

谢谢!

+0

(1)在哪里,你是如何检查的内容'matcher'? (2)'getItem1()'方法从哪里来? – 2013-04-09 20:57:43

回答

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;   
    ................ 
+0

是的,我错过了!谢谢,你为我节省了很多时间。 – user1993412 2013-04-10 01:30:23

0

您初始化匹配一次,然后更改其值。您需要

matching = new Match<String,String>(); 

somwhere在您的循环中。

0

您只是将同一个matching添加到一遍。

如果我是你,我就动

Match<String, String> matching = new Match<String, String>(null, null); 

向右前

matching.setItem1(hospitalPreferred); 
matching.setItem2(studentPreferred); 
matcher.add(matching); 
相关问题