[SalesForce] How to add list of list and list

this is my list of list

((VEC209_ALGE, 2751282.05), (VEC232_ALGE, 2557264.96),
(VEC209_ALGE, 2.0), (VEC232_ALGE, 2.0), 
(VEC209_ALGE, 160000.0), (VEC232_ALGE, 120000.0),
(VEC209_ALGE, 3379000.0), (VEC232_ALGE, 3112000.0), 
(VEC209_ALGE, 3219000.0), (VEC232_ALGE, 2992000.0))

this is my another list

(PVCHT, TAUX1, TVAHRK, PVCDEM, PVCTTC)

I want my output:

((VEC209_ALGE, 2751282.05,PVCHT), (VEC232_ALGE, 2557264.96,PVCHT), (VEC209_ALGE, 2.0,TAUX1), (VEC232_ALGE, 2.0,TAUX1), (VEC209_ALGE, 160000.0,TVAHRK), (VEC232_ALGE, 120000.0,TVAHRK), (VEC209_ALGE, 3379000.0,PVCDEM), (VEC232_ALGE, 3112000.0,PVCDEM), (VEC209_ALGE, 3219000.0,PVCTTC), (VEC232_ALGE, 2992000.0,PVCTTC))

each List(0) should added with List of list(0) and (1)
List(1) added with list of list(2) and (3)

any one give me some idea !!

Best Answer

Consider list1 is your List of List and list2 is your second list from which you want to copy values. You can do the following to get required output:

i = 0, j = 0;
for(i=0; i < list1.size(); i+=2) {
    list1[i].add(list2[j]);
    list1[i+1].add(list2[j]);
    j++;
}

Before using this you should be sure that every time for a List of List of size n there will be another list of size n/2 from which you want to add values to first list. Also you may have to add error handling in the above snippet.

Edit (For variable count value):

i = 0, j = 0, k = 0;
count = 3;  // This value may change
for(i=0; i < list1.size(); i+=count) {
    for(j=i; j<(count+i); j++) {
        list1[j].add(list2[k]);
    }
    k++;
}

See if this works.

Related Topic