【发布时间】:2018-12-21 08:32:43
【问题描述】:
我正在迭代一个以对象为值,以字符串为键的哈希图。
ResultSet myresults = st.executeQuery("SELECT traces.* from traces");
while (myresults.next()) {
MethodTrace MethodTrace = new MethodTrace();
Method method= new Method();
Requirement requirement= new Requirement();
requirement=RequirementHashMap.get(myresults.getString("requirementid"));
method = MethodHashMap.get(myresults.getString("methodid"));
MethodTrace.setMethod(method);
MethodTrace.setRequirement(requirement);
//checking whether the method is present in the superclasses
MethodTrace.setGold(myresults.getString("goldfinal"));
String reqMethod=MethodTrace.Requirement.ID+"-"+MethodTrace.Method.ID;
String reqClass=MethodTrace.Requirement.ID+"-"+MethodTrace.Method.Owner.ID;
//THIS IS THE LINE RESPONSIBLE FOR THE BUG
MethodTrace.Method.Owner.DeveloperGold=classTraceHashMap.get(reqClass).DeveloperGold;
System.out.println(reqMethod+"-");
methodtraceHashMap.put(reqMethod, MethodTrace);
// System.out.println("WE ARE IN THE LOOP "+methodtraceHashMap.get("1-1"));
// System.out.println("WE ARE IN THE LOOP "+methodtraceHashMap.get("1-1"));
}
这是我的代码的简化版本,突出了我的错误的性质和它的确切位置:
for(MethodTrace MethodTrace: methodtraceHashMap2.values()) {
String reqClass=MethodTrace.Requirement.ID+"-"+MethodTrace.Method.Owner.ID;
String reqMethod=MethodTrace.Requirement.ID+"-"+MethodTrace.Method.ID;
//THIS IS THE LINE RESPONSIBLE FOR THE BUG MethodTrace.Method.Owner.DeveloperGold=classTraceHashMap.get(reqClass).DeveloperGold;
methodtraceHashMap2.put(reqMethod, MethodTrace);
System.out.println(methodtraceHashMap2.get("1-1").Method.Owner.getDeveloperGold());
}
我通过从另一个哈希图中检索其值来设置哈希图中每个对象的值,如下行所示
MethodTrace.Method.Owner.DeveloperGold=classTraceHashMap.get(reqClass).DeveloperGold;
我在循环的第一次迭代中将methodtraceHashMap2.get("1-1").Method.Owner.getDeveloperGold() 的值设置为"T",当我移动到循环的第二次迭代时,我的循环设置另一个键的值(键“2-1” ): methodtraceHashMap2.get("2-1").Method.Owner.getDeveloperGold() 到 "N",问题是 methodtraceHashMap2.get("1-1").Method.Owner.getDeveloperGold() 在我的循环的第二次迭代中最终也被设置为 "N" 而它应该是 "T",因为它被设置为 "T"第一次迭代。
这是我的其他课程
public final class MethodTrace {
public static boolean modified = false;
public Method Method= new Method();
public Requirement Requirement=new Requirement();
public String gold;
public String prediction;
public String goldfinal;
public String likelihood;
public String why;
boolean SubjectDeveloperEqualityFlag;
public Methods<String> SuperClassesListMethodTraces;
public Methods<String> InterfaceListMethodTraces;
public Methods<String> ChildrenListMethodTraces;
public Methods<String> ImplementationListMethodTraces;
public boolean TraceSet;
}
public class Method {
public String ID;
public String methodname;
public String fullmethodname;
public Clazz Owner= new Clazz();
public MethodList Callees= new MethodList();
public MethodList Callers= new MethodList();
public MethodList Interfaces= new MethodList();
public MethodList Implementations= new MethodList();
public MethodList Superclasses= new MethodList();
public MethodList Children= new MethodList();
}
public class Clazz {
public String ID;
public String classname;
public String DeveloperGold=new String();
public String SubjectGold;
public List<Clazz> Children= new ArrayList<Clazz>();
public List<Clazz> Parents= new ArrayList<Clazz>();
public List<Clazz> Interfaces= new ArrayList<Clazz>();
public List<Clazz> Implementations= new ArrayList<Clazz>();
public MethodList methods = new MethodList();
}
这里是 MethodTraceHashMap2 的声明:
static LinkedHashMap<String, MethodTrace> methodtraceHashMap = new LinkedHashMap<String, MethodTrace>();
【问题讨论】:
-
如果您使用以小写字符开头的名称作为变量,这将不那么令人困惑。我假设
Method、Owner和DeveloperGold是公共字段,对吗?也许其中任何一个被意外声明为static? -
我同意@Hulk。我假设 Requirement 和 ID 没有定义为静态的。也许也发布这些课程。方法traceHashMap2.put() 不是必需的。您没有在地图中放置另一个 MethodTrace 实例,您只需修改一个属性或它。
-
刚刚添加了它们
-
在迭代时更改集合通常是个坏主意。