This simple method I wrote is an attempt to mimic the equivalent iterative method (I'm just curious about recursion in Apex). It must return the element of a list of custom objects that have the older Createddate.
I printed head and current list value for each recursion layer and the result is correct, but returning to the caller I had a "List out of bounds" error. Since base cases are covered, which further step do I need to do to avoid this error?
Let the size of COset = n, the index in which I got the error is always n-1.
public static customer_Obj__c solveQueueRic(List<customer_Obj__c> COset,Integer index,customer_Obj__c head){
customer_Obj__c lg = COset[index];
if(index == 0){
head = lg;
return solveQueueRic(COset,index+1,head);
}
if(index < COset.size()){
if(lg.Createddate < COset[index].Createddate){
head = lg;
}
return solveQueueRic(COset,index+1,head);
}
return head;
}