Salesforce 运行for循环以获得最小值

Salesforce 运行for循环以获得最小值,salesforce,apex,Salesforce,Apex,这听起来可能有点奇怪,但有人能解释一下下面的循环是如何获得最低值的吗 Decimal LowestPrice; //for loop to find the smallest value for(Integer i = 0;i<leadingPrice.size();i++){ //no idea how the below line is getting the lowest value if(LowestPrice == null || le

这听起来可能有点奇怪,但有人能解释一下下面的循环是如何获得最低值的吗

Decimal LowestPrice;
 //for loop to find the smallest value
    for(Integer i = 0;i<leadingPrice.size();i++){
        //no idea how the below line is getting the lowest value 
        if(LowestPrice == null || leadingPrice.get(i) < LowestPrice){
            lowestprice = leadingPrice.get(i);

        }
十进制最低价格;
//for循环找到最小值
for(整数i=0;i
if(最低价格==null | | | leadingPrice.get(i)

它使用
LowestPrice==null
检查是否分配了LowestPrice。如果分配了LowestPrice,则检查当前索引(
leadingPrice.get(i)
)是否小于LowestPrice。如果
leadingPrice.get(i)
较小,则第一次通过循环将其分配给LowestPrice

,LowestPrice为null(我不喜欢这个假设,但代码就是这样写的)因此,它将LowestPrice设置为数组leadingPrice中的第一个条目。随后每次通过for循环,都会将LowestPrice与leadingPrice数组中的下一个条目进行比较。如果新条目较低,则LowestPrice将更新为新条目。如果不较低,则它将保持当前状态。在整个数组中完成一次旅行后,即为guara我希望有最低的价格


一种更简洁的表述方式是:假设第一个条目是最低的,然后通读整个数组,寻找较低的价格。每次找到一个条目,都会替换当前最低的候选条目。

非常感谢,托马斯,LowestPrice当然是空的,在我观察到的if语句中,这两个条目之间有一个算术比较e十进制和空值(lowsetPrice)但我无法理解null和decimal之间的比较是如何可能的,也无法理解最低价是如何被分配给列表的第一个值的。非常感谢你,托马斯,我已经得到了答案。很高兴你让它起作用了。如果我在写代码,我就不会在新创建的变量上假设null。我已经被在这之前,我会初始化LowestPrice=leadingPrice(0),然后在1处启动for循环。这样,if语句只需要是if(leadingPrice.get(I)if(LowestPrice == null || leadingPrice.get(i) < LowestPrice){ lowestprice = leadingPrice.get(i); }