How to add value to a specific array or list index using apex in Salesforce?

How to add value to a specific array or list index using apex in Salesforce?

add( index, value ) can be used to add value to a specific array or list index using apex in Salesforce.

Sample code:

List < String > strList = new List < String >();  
strList.add( 'Test 0' );  
strList.add( 'Test 1' );  
strList.add( 'Test 2' );  
system.debug( 'Value in 1st block is ' + strList.get( 1 ) );//Test 1  
strList.add( 1, 'Sample 1' );  
system.debug( 'Value in 1st block is ' + strList.get( 1 ) );//Sample 1  

Output:

Leave a Reply