In my testing code, I wrote this piece of code to initialize an account list of size 3:
List<Account> acList = new List<Account>();
for (integer i = 0; i < 3; i++) {
Account ac = new Account();
ac.type = 'Customer';
ac.CountryArea__c = 'Australia';
ac.Vertical_Market__c = 'Education';
ac.T1_Industry__c = 'Other';
acList.add(ac);
}
acList[0].Name = 'Test Account One';
acList[1].Name = 'Test Account Two';
acList[2].Name = 'Test Account Three';
insert acList;
I believe this code will work. But it doesn't seem to be very elegant to me. By adding one Account to the account list every time will cause the list to resize (which means the list will probably destroy the old instance and allocate space for a larger one in background) every time will be of low efficiency. This is an array of 3 so will not be a big deal. But is there a better way to write this? Appending to a set will definitely increase efficiency, but will not allow me to use acList[0].