 ShowAll Questionssorted byDate Posted pooja chauchan

# Calculations on a list

Hello everyone,

I am trying to do some data manipulation over records in a list. I was able to correctly build my list but now I am stuck with the calculations that I want to see happen.

Let's say I have record 1 and record 2 in my list. I want to grab a field called meter readings on both record and subtract the value of record 2 from the value from record 1 to get the actual energy use.

record 1 energy use = record 1 meter reading - record 2 meter reading.

And obvisouly I would like to repeat that for every records in my list except for the last one (since I would not have a second meter reading to do the calculation).

At first I thought of using a variable to control the for loop but I keep getting the following error message: "System.ListException: List index out of bounds: 15" Best Answer chosen by pooja chauchan Gaurav Nirwal
```List<Item__c> currentItemList = meterToItemMap.get(currentMeter);
if(currentItemList ! = null && currentItemList.size()>)
{
For( Integer i =0; i <currentItemList.size(); i++)
{
Integer x = i+1;
if(currentItemList.size()> x){
}
}``` Gaurav Nirwal
```List<Item__c> currentItemList = meterToItemMap.get(currentMeter);
if(currentItemList ! = null && currentItemList.size()>)
{
For( Integer i =0; i <currentItemList.size(); i++)
{
Integer x = i+1;
if(currentItemList.size()> x){
}
}```
This was selected as the best answer Wizno @ Configero
So as I understand it, the 1st record is the "initial" reading, and all subsequent records will be subtractions from the first?

```Meter__c[] readings = [SELECT Id, Reading__c, Reading_Date__c FROM Meter__c WHERE Account__c = 'Some_Assumed_Parent_Record' ORDER BY Reading_Date__c ASC];

for( Integer x =1; x<readings.size(); x++){ //Since we already for the first reading to set as the default, we can skip item 0
}

/*
The above is a basic example, if you need to do multiple readings, it would look something like this
*/

public class MeterWrapper {

}