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
Siana DcruzSiana Dcruz 

Values are not displaying in proper list indices!!

Hey all,
I am stuck in my code where i need help in lists
I have my code something like this.
List<String[]> rowAlter= new List<String[]>(size);
            
            Integer i=1,j=2;
          for(String[] r : csv1) {
             system.debug('odd-->'+r+'!!!!!---'+i);
              rowAlter.add(i,r);
             system.debug('odd rowalter--'+i+'--'+rowAlter[i]);
              i=i+2;
         }
         system.debug('before rowAlter[3]= '+rowAlter[3]);
         for(String[] s : csv2) {
         system.debug('even-->'+s+'!!!!!---'+j);
              rowAlter.add(j,s);
              system.debug('even rowalter--'+j+'--'+rowAlter[j]);
              j=j+2;
              
         }

when i check debug logs for (system.debug('odd rowalter--'+i+'--'+rowAlter[i]);)
all the values are correctly placed in odd indices.

but again when the control comes in the for loop for even indices,all the values are correctly placed for even indices but odd indices values are not in order and few of them are null.I tried a lot  to replicate the issue but couldn't.I really need a help in this.Please can anyone help me resolve it?
Apoorv Saxena 4Apoorv Saxena 4
Hi Siana,'

You need to do something like this :
 
List<String> csv1 = new List<String>{'a','c','e'};
List<String> csv2 = new List<String>{'b','d','f'};

List<String> rowAlter= new List<String>(10);
            
Integer i=0,j=1;
for(String r : csv1) {
	rowAlter.add(i,r);
	i=i+1;
}

for(String s : csv2) {
	rowAlter.add(j,s);
	j=j+2;  
}
system.debug('Final: '+rowAlter);

Adjust the values of 'i' & 'j'  and size(here i have harcoded size as 10) as per your requirement.

Please mark this question as Solved if this helps you so that others can view it as a proper solution.

Thanks,
Apoorv