0

我有 summariseData() 方法并调用了很多时间来检索值。但第一次是工作文件,但第二次执行值在 HashMap 中增加。

    void summarizeData() {

    HashMap outerMap = new HashMap();

    ArrayList list = new ArrayList(dataClass.getData());


    for (int indx = 0; indx < list.size(); indx++) {
        System.out.println("indx : " + indx);
        Resultset rs = new Resultset();
        rs = (Resultset) list.get(indx);


        if (rs != null) {

            int id = rs.getTestCaseNumber();
            if (id > 0) {
                Object isExists = outerMap.get(id);

                if (isExists != null) {
                    //System.out.println("found entry so updating");
                    Resultset inRs = new Resultset();
                    inRs = (Resultset) isExists;

                    if (inRs != null) {
                        int totExec = inRs.getTestExecution();
                        int totPass = inRs.getTestCasePass();
                        int totFail = inRs.getTestCaseFail();

                        //     System.out.println("totE :" + totExec + "  totP:" + totPass + "  totF:" + totFail);

                        int newRsStat = rs.getTestCasePass();

                        if (newRsStat == 1) {
                            totPass++;
                            inRs.setTestCasePass(totPass);
                        } else {
                            totFail++;
                            inRs.setTestCaseFail(totFail);
                        }
                        totExec++;

                        //      System.out.println("id : "+id+"  totPass: "+totPass+"  totFail:"+totFail);
                        //       System.out.println("key : " + id + "  val : " + inRs.getTestCaseNumber() + " " + inRs.getTestCasePass() + "  " + inRs.getTestCaseFail());

                        inRs.setTestExecution(totExec);
                        outerMap.put(id, inRs);
                    }

                } else {

                    //    System.out.println("not exist so new entry" + " totE:" + rs.getTestExecution() + "  totP:" + rs.getTestCasePass() + "  totF:" + rs.getTestCaseFail());
                    outerMap.put(id, rs);
                }
            }
        } else {
            System.out.println("rs null");
        }


    }

第一次执行时的输出:

indx : 0
indx : 1
indx : 2
indx : 3
indx : 4
indx : 5
indx : 6
indx : 7
indx : 8
indx : 9
indx : 10
totE :1  totP:1  totF:0
indx : 11
totE :1  totP:1  totF:0
indx : 12
totE :1  totP:1  totF:0
indx : 13
totE :1  totP:1  totF:0
indx : 14
totE :1  totP:1  totF:0
indx : 15
totE :1  totP:1  totF:0
indx : 16
totE :1  totP:1  totF:0
indx : 17
totE :1  totP:1  totF:0
indx : 18
totE :1  totP:1  totF:0
indx : 19
totE :1  totP:1  totF:0

第二次执行时的输出:

indx : 0
indx : 1
indx : 2
indx : 3
indx : 4
indx : 5
indx : 6
indx : 7
indx : 8
indx : 9
indx : 10
totE :2  totP:2  totF:0
indx : 11
totE :2  totP:2  totF:0
indx : 12
totE :2  totP:2  totF:0
indx : 13
totE :2  totP:2  totF:0
indx : 14
totE :2  totP:2  totF:0
indx : 15
totE :2  totP:2  totF:0
indx : 16
totE :2  totP:2  totF:0
indx : 17
totE :2  totP:2  totF:0
indx : 18
totE :2  totP:2  totF:0
indx : 19
totE :2  totP:2  totF:0

而我每次执行都需要相同的输出。

4

1 回答 1

2

值正在递增,因为在以下行中:

totPass++;
inRs.setTestCasePass(totPass);

totFail++;
inRs.setTestCaseFail(totFail);

totExec++;
inRs.setTestExecution(totExec);

您正在通过引用变量增加testCasePass,testcaseFail和值,该变量反映在. 之所以如此,是因为所有变量(并且共享同一个对象,即对象 at )。这就是为什么每次调用该方法时,您都会获得字段的递增值。testexecutioninRsisExistsouterMaprsinRsouterMap.get(id)summarizeData

更新
为了克服这个问题,您应该在ResultSet类中使用复制构造函数,如下所示:

public ResultSet(ResultSet rs)
{
    testCasePass = rs.getTestCasePass();
    testCaseFail = rs.getTestCaseFail();
    testExecution = rs.getTestExecution();
}

当您创建时inRs,请使用以下行:

ResultSet inRs = new ResultSet(isExists);

更新

不要放inRsoutperMap

于 2013-06-28T10:41:09.063 回答