0

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 !!

0

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.

| improve this answer | |
  • Thank you for the answers.It helps me a lot.I need some more clarification. listref size is always 5 listref=((PVCHT, TAUX1, TVAHRK, PVCDEM, PVCTTC)) listPriceref is a List<list<string>> .This size differs. listPriceref=((VEC209_ALGE, 2751282.05,PVCHT), (VEC232_ALGE, 2557264.96,PVCHT), (VEC209_ALGE, 2.0,TAUX1),........) I have to map the listref(0) to ListPriceref(size) Eg:If size is 5 i have to map listref(0) to ListPriceref(0,1,2,3,4) listref(1) to ListPriceref(5,6,7,8,9) this can be done ???!!! – uma451 Feb 15 '16 at 9:58
  • How do you want to distribute those 5 values in listref to lists in listPriceref? Is there any rule which defines how these 5 values will be distributed? @uma – ajinkyah Feb 15 '16 at 10:02
  • I mean, In your example, how is size = 5 decided? – ajinkyah Feb 15 '16 at 10:04
  • lisref size is always 5.it's somewhat like predefined.the listref values may change but size is 5.I want distribute those 5 values to lists in listPriceref.depending upon some integer count.Is it clear. ex:integer count 3 listref(0) added to LIstPriceref(0),LIstPriceref(1),LIstPriceref(2) then listref(1) added to LIstPriceref(4),LIstPriceref(5),LIstPriceref(6) If integer count 4 Listref(0) added to LIstPriceref(0),LIstPriceref(1),LIstPriceref(2),LIstPriceref(3) here count is some integer value – uma451 Feb 15 '16 at 10:19
  • updated the answer @uma – ajinkyah Feb 15 '16 at 11:51
0
  1. Loop through the first list.
  2. Get the index
  3. Fetch the same index data from second list (say tmp)
  4. Append the tmp to list in the index of first list

Edit:

    for (Integer index = 0; index < firstList.size(); index++) {
        List<Object> tmp = firstList.get(index);
        tmp.add(secondList.get(index));
    }
| improve this answer | |
0

Try this one and do some modification.

List<List<String>> wew= new List<List<String>>();
List<String> wew2= new List<String>();
wew.add(new List<String>{'VEC209_ALGE', '2751282.05'});
wew.add(new List<String>{'VEC232_ALGE', '2557264.96'});
wew.add(new List<String>{'VEC232_ALGE', '120000.0'});
wew.add(new List<String>{'VEC232_ALGE', '3112000.0'});

wew2.add('PVCHT');
wew2.add('TAUX1');
Integer x = 1;
for(List<String> hehe : wew) {

    if(math.mod(x,2) == 0){
        hehe.add(wew2[0]);        
        System.debug('The Final Result' + hehe);
        wew2.remove(0);
    } else {
         hehe.add(wew2[0]);        
        System.debug('The Final Result' + hehe);
    }
    x++;

}
| improve this answer | |
  • Are you assuming '2751282.05' will be stored as String inside wew.add(new List<String>{'VEC209_ALGE', '2751282.05'}); ? – karthikselva Feb 15 '16 at 9:06
  • @karthikselva Based on the given example, Yes. But that is only an example. I just want to show some possible way to solve his problem. – Hope Feb 15 '16 at 9:11
  • Thank you for the answers.I need some more clarification. listref size is always 5 listref=((PVCHT, TAUX1, TVAHRK, PVCDEM, PVCTTC)) listPriceref is a List<list<string>> .This size differs. listPriceref=((VEC209_ALGE, 2751282.05,PVCHT), (VEC232_ALGE, 2557264.96,PVCHT), (VEC209_ALGE, 2.0,TAUX1),........) I have to map the listref(0) to ListPriceref(size) Eg:If size is 5 i have to map listref(0) to ListPriceref(0,1,2,3,4) listref(1) to ListPriceref(5,6,7,8,9) this can be done ???!!! – uma451 Feb 15 '16 at 9:48
0

try this...

List<List<String>> mList= new List<List<String>>();
mList.add(new List<String>{'VEC209_ALGE', '2751282.05'});
mList.add(new List<String>{'VEC232_ALGE', '2557264.96'});
mList.add(new List<String>{'VEC209_ALGE', '2.0'});
mList.add(new List<String>{'VEC232_ALGE', '2.0'});
mList.add(new List<String>{'VEC209_ALGE', '160000.0'});
mList.add(new List<String>{'VEC232_ALGE', '120000.0'});
mList.add(new List<String>{'VEC209_ALGE', '3379000.0'});
mList.add(new List<String>{'VEC232_ALGE', '3112000.0'});
mList.add(new List<String>{'VEC209_ALGE', '3219000.0'});
mList.add(new List<String>{'VEC232_ALGE', '2992000.0'});
Integer counter = 0;
Integer mcounter = 0;
List<String> mList2 = New List<String>{'PVCHT', 'TAUX1', 'TVAHRK', 'PVCDEM', 'PVCTTC'};
    for (List<String> mLString : mList){
        counter += 1;
        if (counter ==1){
             mLString.add(mList2[mcounter]);
        }
        if (counter ==2) {
            mLString.add(mList2[mcounter]);
            counter =0;
            mcounter +=1;
        }
        system.debug(mLString);
    }
| improve this answer | |
-1

Use 2 Dimensional List instead:

List<List<string>> arrayOfReportData = new List<List<string>>();
    for(Integer i=0; i<3; i++){
        arrayOfReportData.add(new List<String>{'MyValue1', i});
    }

The Output will be : ((MyValue1,0), (MyValue1, 1), (MyValue1, 2))

Follow the link for more details:

https://developer.salesforce.com/forums/?id=906F00000005GOrIAM

| improve this answer | |
  • While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – identigral Apr 15 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.