当前位置: 首页 > 知识库问答 >
问题:

如何修复声纳问题鱿鱼:forloopcounterchangedcheck?

缪朝
2023-03-14
int limit = Const.getBatchLimit();
int count = 0;

for (int paramIndex = 0; paramIndex < paramList.size();)
{
    List<Param> tempParam = new ArrayList<>();
    StringBuilder retrieveMsg = new StringBuilder("Retrieving: \n");
    for (count = 0; (count < limit) && (paramIndex < param.size()); count++, paramIndex++)
    {
        Param myParam = paramList.get(paramIndex);
        retrieveMsg.append(myParam .seq()).append(Str.SLASH).append(Str.CRLF);
        tempParam.add(myParam);
    }
    LOGGER.info(retrieveMsg.toString());

    LOGGER.info("Done. Retrieved count: " + tempParam.size());
}

我不能将Paramindex++移动到第一个for循环,因为行为将与所需的不同。

如果有人能就如何修复这种违规行为提出建议,我将不胜感激。提前谢谢你。

共有1个答案

养星汉
2023-03-14

将参数列表拆分为limit大小的块是一种有趣的方法,但是完全可以去掉嵌套循环

另外,如果tempparam仅用于跟踪批处理中的元素数,则可以通过重用count来替换它。

int limit = Const.getBatchLimit();

StringBuilder retrieveMsg = new StringBuilder("Retrieving: \n");

for (int i = 0, count = 1, n = paramList.size(); i < n; i++, count++) {
    Param myParam = paramList.get(i);
    retrieveMsg.append(myParam.seq()).append(Str.SLASH).append(Str.CRLF);

    if (count % limit == 0 || i == n - 1) {
        LOGGER.info(retrieveMsg.toString());
        LOGGER.info("Done. Retrieved count: " + count);
        retrieveMsg = new StringBuilder("Retrieving: \n");
        count = 0;
    }
}

或者可以完全重构此代码,以使用Java Stream API,并实现一个helper方法来处理块(例如printchunk):

int chunks = paramList.size() / limit + (paramList.size() % limit == 0 ? 0 : 1);
IntStream.range(0, chunks)
         .mapToObj(i -> paramList.subList(i * limit, Math.min((i + 1) * limit, paramList.size()))) // getting stream of sublists
         .forEach(MyClass::printChunk);

// ...
static void printChunk(List<Param> chunk) {
    StringBuilder msg = new StringBuilder("Retrieving:\n");
    chunk.forEach(p -> msg.append(p.seq()).append(Str.SLASH).append(Str.CRLF));

    LOGGER.info(msg.toString());
    LOGGER.info("Done. Retrieved count: " + chunk.size());
}

 类似资料:
  • 我在Eclipse中运行声纳分析时遇到了一些问题。我有一个带有Groovy Spock单元测试Java Maven项目。 从所有这些代码中,我了解到sonar-eclipse-groovy三角形存在一些问题。任何提示都非常感谢! 编辑_______________我将GGTS重新安装到一个非共享文件夹(也称为“C:\tools”,而不是“C:\program files”)。Sonar插件使用我的

  • 我有一个代码片段,在那里我从sonar得到了关键的代码气味,因为exception实现了Serializable。这段代码在生产中运行了很长一段时间,我从Sonar Doc中看不到任何问题:“例如,在负载下,大多数J2EE应用程序框架将对象刷新到磁盘,一个据称具有非瞬时、不可串行化数据成员的可串行化对象可能导致程序崩溃”。你是否在你的应用程序中面对这个声纳问题的代码崩溃?

  • 我使用的是Selenium3.9.0+Geckodriver0.24+Firefox58.0.2。 当webdriver想要在我的目标站点上单击导航树中的元素时->script crashing with selenium异常:“selenium.Common.Exceptions.ElementClickInterceptedException:Message:element is not cl

  • 我们在服务器和客户机模式下使用Ignite 2.7.6:两个服务器和六个客户机。 正如我们所看到的,现在所有服务器节点的CPU负载都很高,约为250%(更新前为20%),而长G1 Young Gen的停顿时间高达5毫秒(更新前为300微秒)。 服务器配置为: 在Ignite服务器节点的内存转储中,我们看到大量,大小为21MB

  • 下面是正在讨论的方法。 完整的错误如下。 使用 对于 Sonar 来说还不够吗? 更新:向方法中添加了缺失但相关的代码。现在没有代码丢失。

  • parent-child1-pom.xml-child2-pom.xml-child3-pom.xml-pom.xml 我在父pom.xml中包含了JaCoCo插件。当我从父级pom.xml运行mvn clean install sonar:sonar build时,我看到每个子级都生成自己的jacoco.exec文件。类似于child1/target/jacoco.exec,child2/tar