public static ListInteger getMinValueIndexesListBigDecimal bigDecimals ListInteger indexes = new ArrayList; 初始化最小值为列表中的第一个元素 BigDecimal minValue = bigDecimalsget0; 遍历列表找到最小值 for int
public static List
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;
}
优化说明:
- 将注释去掉,代码的可读性更好。
- 在for循环中,使用else if语句来判断当前元素是否等于最小值,避免不必要的比较。
- 优化后的代码逻辑保持不变,但是更加简洁和清晰
原文地址: https://www.cveoy.top/t/topic/is6m 著作权归作者所有。请勿转载和采集!