public static List getMinValueIndexes(List bigDecimals) { List indexes = new ArrayList<>();

BigDecimal minValue = bigDecimals.get(0);

for (int i = 1; i < bigDecimals.size(); i++) {
    BigDecimal current = bigDecimals.get(i);
    int comparison = current.compareTo(minValue);

    if (comparison < 0) {
        minValue = current;
        indexes.clear();
    } else if (comparison == 0) {
        indexes.add(i);
    }
}

return indexes;

}

优化说明:

  1. 将注释去掉,代码的可读性更好。
  2. 在for循环中,使用else if语句来判断当前元素是否等于最小值,避免不必要的比较。
  3. 优化后的代码逻辑保持不变,但是更加简洁和清晰
public static ListInteger getMinValueIndexesListBigDecimal bigDecimals ListInteger indexes = new ArrayList; 初始化最小值为列表中的第一个元素 BigDecimal minValue = bigDecimalsget0; 遍历列表找到最小值 for int

原文地址: https://www.cveoy.top/t/topic/is6m 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录