gpt4 book ai didi

java - 如何解决 Sonar 问题 "Correctness - This method continues a loop after finding an equality condition "

转载 作者:行者123 更新时间:2023-12-01 17:31:23 25 4
gpt4 key购买 nike

我有以下代码。我想要从代码中得到的只是

  1. 如果方法参数中提供的语言环境存在于 labelList 中,则返回它或
  2. 检查 labelList 中是否存在 EN_US,然后返回
 String LOCALE = "locale"; 
List<Map<String, Object>> labelList = // coming for other class
...
public Object resolveData(String locale) {
Map<String, Object> enUSLabel = null;
locale = locale!= null && !locale.isEmpty() ? locale: EN_US;
for (Map<String, Object> picklistLabel : labelList) {
if (picklistLabel.get(LOCALE).equals(locale)) {
return picklistLabel;
} else if (EN_US.equals(picklistLabel.get(LOCALE))) {
enUSLabel = picklistLabel;
}
}
return enUSLabel;
}

我遇到 Sonar 问题(在 enUSLabel = picklistLabel;)

正确性 - 该方法在找到相等条件后继续循环在基于相等性的 if 条件中查找并设置变量后,此方法继续循环,并且不会中断循环。由于继续循环似乎不太可能再次找到该项目,因此在此时中断似乎是正确的操作。

如何纠正?是否可以在一个 for 循环中实现?

最佳答案

enUSLabel = picklistLabel 替换为 return picklistLabel

关于java - 如何解决 Sonar 问题 "Correctness - This method continues a loop after finding an equality condition ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61115869/

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