Instead of adding an element, sometimes we'll want to replace an element. You can use the square bracket notation []
to replace an element at a specific index:
List<Decimal> amountList = new List<Decimal>{200.50, 300.99}; amountList[0] = 450.99; Decimal firstIndex = amountList[0]; // 450.99 Decimal secondIndex = amountList[1]; // 300.99
Or you can use the set(index, element)
instance method to do the same thing:
List<Decimal> amountList = new List<Decimal>{200.50, 300.99}; amountList.set(0, 450.99); Decimal firstIndex = amountList[0]; // 450.99 Decimal secondIndex = amountList.get(1); // 300.99