首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用列表进行迭代时正确刷新列表

如何在使用列表进行迭代时正确刷新列表
EN

Stack Overflow用户
提问于 2015-08-19 21:47:40
回答 2查看 235关注 0票数 2

我有以下问题。我正在从SonicWall中删除一些防火墙规则。我这样做的方法是从sonic wall中获取所有规则,找到匹配的规则,将它们存储在一个列表中,并使用这个列表来发出删除它们的命令。删除规则的方法是使用SSH和带有ID的命令。我遇到的问题是,每次删除ID时,所有ID都会更改,导致第二个命令尝试删除不存在的访问规则(或者更糟糕的是错误的访问规则)。为了克服这一点,在每次删除过程之后,我刷新列表i,以获得更新后的元素以及它们的新ID。我面临的问题是列表的更新是不正确的(它发生在for each循环中),因为列表被用作迭代器(导致发出错误的命令)。

用新元素更新这个列表的最好和最安全的方法是什么?

下面是有问题的代码部分:

代码语言:javascript
复制
multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));     
        accessRules = getAccessRules(multiResult, accessRule);
        if(accessRules == null){
            Trc.trc("No access rules found to delete");
            return false;
        }

        numberOfRulesToDelete = accessRules.size();
        int size = numberOfRulesToDelete;
        IntList deleteRulesId = new IntList();

        while(numberOfRulesToDelete != 0){
            for(AccessRule arToDelete : accessRules){
            expect.sendLine("configure");
            multiResult = expect.expect(anyOf(regexp("(.*)" + confmodePromptPattern)));
            Result showCmdResult = multiResult.getResults().get(0);
            GenericCmdDto outPut = null;
            ObjectMapper objectMapper = new ObjectMapper();
            GenericFailedCmdDto failedOutPut = null;
            String swResponse = null;

            swResponse = handleSWConfModeResponse(showCmdResult);
            outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
            if(outPut.getSuccess()){//if change to configure command was successfull
                Result confCmdResult = null;
                Trc.trc("Configure command: " + outPut.getSuccess());
                expect.sendLine("no access-rule id "  + arToDelete.getIndex());
                MultiResult confResult = expect.expect(anyOf(regexp("(.*)" + confmodePromptPattern)));
                confCmdResult = confResult.getResults().get(0);

                swResponse = handleSWConfModeResponse(confCmdResult);
                outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
                if(outPut.getSuccess()){//if the delete command was successfull
                Trc.trc("Delete command issued successfully");
                expect.sendLine("commit");
                confCmdResult = expect.expect(anyOf(regexp("(.*)" + confmodePromptPattern)));

                swResponse = handleSWConfModeResponse(confCmdResult);
                outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
                if(outPut.getSuccess()){//if the commit command was successfull
                    Trc.trc("Commit command issued succesfully!");
                    Trc.trc("Deleted successfully access rule: " + arToDelete.getIndex());
                    deleteRulesId.addElement(arToDelete.getIndex());

                    expect.sendLine("exit");//change to no conf mode
                    confCmdResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));

                    swResponse = handleSWPromptModeResponse(confCmdResult);
                    outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
                    if(outPut.getSuccess()){//if the exit command was successfull
                    Trc.trc("Exit command issued successfully!");
                    operationSuccess = outPut.getSuccess();
                    }else{//if exit unsucessfull                
                    failedOutPut = objectMapper.readValue(createFailedJSONResponse(swResponse.getBytes()), GenericFailedCmdDto.class);
                    operationSuccess = outPut.getSuccess();
                    Trc.trc("Commit command has failed! Message: " + failedOutPut.getMessage() + ", Code: " + failedOutPut.getCode());
                    Trc.trc("Changing to show mode....");
                    expect.sendLine("exit");
                    multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
                    showCmdResult = multiResult.getResults().get(0);

                    swResponse = handleSWPromptModeResponse(confCmdResult);

                    outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
                    if(outPut.getSuccess()){
                        Trc.trc("Exiting from conf mode..");
                        Trc.trc("Exit command: " + outPut.getSuccess());
                    }
                    return operationSuccess;
                    }
                }else{//if commit unsucessfull
                    failedOutPut = objectMapper.readValue(createFailedJSONResponse(swResponse.getBytes()), GenericFailedCmdDto.class);
                    operationSuccess = outPut.getSuccess();
                    Trc.trc("Commit command has failed! Message: " + failedOutPut.getMessage() + ", Code: " + failedOutPut.getCode());
                    Trc.trc("Changing to show mode....");
                    expect.sendLine("exit");
                    multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
                    showCmdResult = multiResult.getResults().get(0);

                    swResponse = handleSWPromptModeResponse(showCmdResult);
                    outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
                    if(outPut.getSuccess()){
                    Trc.trc("Exiting from conf mode..");
                    Trc.trc("Exit command: " + outPut.getSuccess());
                    }
                    return operationSuccess;
                }
                }else{//if delete unsucessfull
                failedOutPut = objectMapper.readValue(createFailedJSONResponse(swResponse.getBytes()), GenericFailedCmdDto.class);
                operationSuccess = outPut.getSuccess();
                Trc.trc("Commit command has failed! Message: " + failedOutPut.getMessage() + ", Code: " + failedOutPut.getCode());
                Trc.trc("Changing to show mode....");
                expect.sendLine("exit");
                multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
                showCmdResult = multiResult.getResults().get(0);

                swResponse = handleSWPromptModeResponse(showCmdResult);
                outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
                if(outPut.getSuccess()){
                    Trc.trc("Exiting from conf mode..");
                    Trc.trc("Exit command: " + outPut.getSuccess());
                }
                return operationSuccess;
                }

            }
            else{//if configure unsuccessfull
                failedOutPut = objectMapper.readValue(createFailedJSONResponse(swResponse.getBytes()), GenericFailedCmdDto.class);
                operationSuccess = outPut.getSuccess();
                Trc.trc("Commit command has failed! Message: " + failedOutPut.getMessage() + ", Code: " + failedOutPut.getCode());
                Trc.trc("Changing to show mode....");
                expect.sendLine("exit");
                multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
                showCmdResult = multiResult.getResults().get(0);

                swResponse = handleSWPromptModeResponse(showCmdResult);
                outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
                if(outPut.getSuccess()){
                Trc.trc("Exiting from conf mode..");
                Trc.trc("Exit command: " + outPut.getSuccess());
                }
                return operationSuccess;
            }     
            expect.sendLine("show access-rules from " + accessRule.getFromZone() + " to " + accessRule.getToZone());
            multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
            accessRules = getAccessRules(multiResult, accessRule);
            if(accessRules != null){
                numberOfRulesToDelete = accessRules.size();
            }else{
                Trc.trc("Deleting process complete!");
                numberOfRulesToDelete = 0;
                for(int i = 0; i < deleteRulesId.size(); i++){
                Trc.trc("Deleted " + size + " successfully rules: " + deleteRulesId.getElementAt(i));
                }
            }
            }  
        }
        return operationSuccess;
EN

回答 2

Stack Overflow用户

发布于 2015-08-19 21:52:36

你不应该在遍历列表的时候更新它。

请使用副本。

票数 1
EN

Stack Overflow用户

发布于 2015-08-19 22:09:15

使用列表的迭代器会对您有所帮助吗?您可以使用迭代器delete方法并立即从列表中移除该项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32097396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档