function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
David JoshuaDavid Joshua 

How to add elements properly on specific positions to an empty list ?

Hi, 

Why can I do 
        Integer val_media_deliveries = 66;
        Integer val_impressions = 55;
        Integer val_clips = 44;
        
        List<Integer> valuesToReturn = new List<Integer>(1);
        
        valuesToReturn.add(0,val_media_deliveries);
        valuesToReturn.add(1,val_impressions);
        valuesToReturn.add(2,val_clips);

        return valuesToReturn;
But not : 
        Integer val_media_deliveries = 66;
        Integer val_impressions = 55;
        Integer val_clips = 44;
        
        List<Integer> valuesToReturn = new List<Integer>(0);
        
        valuesToReturn.add(0,val_media_deliveries);
        valuesToReturn.add(1,val_impressions);
        valuesToReturn.add(2,val_clips);

        return valuesToReturn;
(because I get this error : )
{faultcode:'soapenv:Client', faultstring:'System.ListException: List 
index out of bounds: 0

Class.FranceOperations_TargetDistri.parse: line 32, column 1
Class.FranceOperations_TargetDistri.load: line 80, column 1
Class.FranceOperations_ReloadImpressions.getTotalImpressions: line 60, column 1',}
I would my result to be : 
my values : 66,55,44

but not :
my values : 66,55,44,

What is it I am missing ? ​​​​​​​



 
Raj VakatiRaj Vakati
Change it as 


 
Integer val_media_deliveries = 66;
Integer val_impressions = 55;
Integer val_clips = 44;

List<Integer> valuesToReturn = new List<Integer>();

valuesToReturn.add(0,val_media_deliveries);
valuesToReturn.add(1,val_impressions);
valuesToReturn.add(2,val_clips);

return valuesToReturn;

 
David JoshuaDavid Joshua
Thanks for your quick anwser Raj. 
I tried that too, but same error. 
List index error out of bounds: 0
 
Raj VakatiRaj Vakati
Try this .. 
 
Integer val_media_deliveries = 66;
Integer val_impressions = 55;
Integer val_clips = 44;

List<Integer> valuesToReturn = new List<Integer>(1);

valuesToReturn.add(0,val_media_deliveries);
valuesToReturn.add(1,val_impressions);
valuesToReturn.add(2,val_clips);
System.debug(valuesToReturn);



or

 
Integer val_media_deliveries = 66;
Integer val_impressions = 55;
Integer val_clips = 44;

List<Integer> valuesToReturn = new List<Integer>();

valuesToReturn.add(val_media_deliveries);
valuesToReturn.add(val_impressions);
valuesToReturn.add(val_clips);
System.debug(valuesToReturn);

 
David JoshuaDavid Joshua
The first sample of code is the one that I used in my 1st post. But as I said, I would like my result to be : 
my values : 66,55,44
and not :
my values : 66,55,44,
And I have tried the second one too, but I want to assigne my values on fixed positions. That way, I know for certain for example val_media_deliveries is on position [0]. 

 
David JoshuaDavid Joshua
Ok, i found that solution : 
        Integer val_media_deliveries = 66;
        Integer val_impressions = 55;
        Integer val_clips = 44;
        
        List<Integer> valuesToReturn = new List<Integer>(3);
        
        valuesToReturn.set(0,val_media_deliveries);            
        valuesToReturn.set(1,val_impressions);
        valuesToReturn.set(2,val_clips);
        
        return valuesToReturn;
And return is : 
my values : 66,55,44
Raj VakatiRaj Vakati
My mistake .. you are correct .. when you are using add method with index you need to set the length more that or equals size