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
Syed F Raza 10Syed F Raza 10 

I am receiving error : System.ListException: List index out of bounds: 0

Hi All,

The following tag is executing as per requirement:
 
List<integer> list1= new List<integer>();
list1.add(10);
list1.add(20);
list1.add(30);
list1.add(40);
system.debug('Value of List after increase is '+list1);

However if I use the list.add(index,element) method to add elements I am receivng error System.ListException: List index out of bounds: 0

The code I am having error with is
 
List<integer> list1= new List<integer>();
list1.add(0,10);
list1.add(1,20);
list1.add(2,30);
list1.add(3,40);
system.debug('Value of List after increase is '+list1);
Please advise.

Thanks!!
 
Maharajan CMaharajan C
Hi Syed,

In Apex we have the different syntax for this:
 
List<Integer> list1 = new Integer[4];
list1.add(0,10);
list1.add(1,20);
list1.add(2,30);
list1.add(3,40);
system.debug('Value of List after increase is '+list1);

Refer the List Methods from below document:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm

Thanks,
Maharajan.C