gpt4 book ai didi

java - 使用不正确的值填充 HashMap。

转载 作者:行者123 更新时间:2023-12-02 03:07:36 24 4
gpt4 key购买 nike

我使用 Reflection API 来计算类的传入和传出耦合,然后计算每个类的稳定性指标。然后将结果添加到 HashMap 中。当我运行调试器时,程序似乎运行正确,并且似乎将正确的值传递给 HashMap。

当我检查方法的返回语句(这是映射)时,键是正确的(Key是一个类),值(Value是测量对象)不正确。该值始终是其中一个接口(interface)的结果。

本地图在计算后返回时,它具有正确的键,但与每个键关联的值不正确。每个Key的值是相同的

 public ClassMap getEfferent(ClassList list){
map = new ClassMap();
measure = new Measurement();

//cycle through the list
for (int i = 0; i < list.size(); i++) {

//Get the class needed for efferent inspection
Class cla = list.getMyClass(i);

//get the interfaces from the class
Class[] interfaces = cla.getInterfaces();

//if the class implements any interfaces increment efferent
for(Class inter : interfaces){

//if the interface is part of the list
if(list.contains(inter)){
efferentCoupling++;
}

}//end interfaces

Constructor[] cons = cla.getConstructors();
Class[] conParams;

for(Constructor c: cons){

conParams = c.getParameterTypes();

for(Class par: conParams){

//if the paramater name is on the list of classes ++
if(list.contains(par.getName())){
efferentCoupling ++;
}

}

}//end Constructor params

Field[] fields = cla.getFields();

for(Field fie: fields ){

//if the field name is on the list of classes ++
if(list.contains(fie.getName()))
efferentCoupling ++;


}//fields

//get the methods for the class
Method[] classMethods = cla.getMethods();
Class[] params;

//
for(Method meth: classMethods){

Class returnTypes = meth.getReturnType();

if(list.contains(meth.getReturnType().getName())){
efferentCoupling ++;
}

}

//pass in the list and the class name to check for afferent coupling
//return the afferent score as an interger
afferentCoupling = getAfferent(list, cla.getName());

Name = cla.getName();

//pass the the class name into setClassName
measure.setClassName(Name);

//pass in the efferentCoupling for the class
measure.setEfferentCoupling(efferentCoupling);
//pass in the afferentCoupling for the class
measure.setAfferentCoupling(afferentCoupling);

//System.out.println(measure.getStability());
cla = list.getMyClass(i);

//put the class(key) measure (value)
map.put(cla, measure);

}//end for


//resets efferent coupling
efferentCoupling = 0;

return map;
}//getAfferent


//method passes in the ClassList and the class name to be checked
public int getAfferent(ClassList list, String name){
int afferent = 0;

for (int i = 0; i < list.size(); i++) {

//Get the class needed for afferent inspection
Class cla = list.getMyClass(i);

Class[] interfaces = cla.getInterfaces();

//if the class implements any interfaces increment efferent
for(Class inter : interfaces){

//if the interface name is same as inter.getName() then increment afferent
if(inter.getName() == name){
afferent ++;
}

}//end interfaces

Constructor[] cons = cla.getConstructors();
Class[] conParams;

for(Constructor c: cons) {

conParams = c.getParameterTypes();

for (Class par : conParams) {

//if constructor params == name then increment
if (par.getName() == name) {
afferent++;
}
}
}

Field[] fields = cla.getFields();
for(Field fie: fields ){

if(fie.getName() == name)
afferent++;
}//fields

Method[] classMethods = cla.getMethods();
Class[] params;

for(Method meth: classMethods){

Class returnTypes = meth.getReturnType();

if(meth.getReturnType().getName() == name){

afferent ++;
}

}
}

return afferent;
}

任何帮助都会很棒。

最佳答案

你必须输入

measure = new Measurement();

在 for 循环内。

目前,您仅创建一个测量并在循环中多次修改+添加它。因此,所有键都将指向同一个Measurement对象(可能包含循环最后一次迭代的数据。)

关于java - 使用不正确的值填充 HashMap。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41547369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com