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
Vijay@sfdcVijay@sfdc 

Looping nested list


Hello all, 
  please help me to display indiviusal elements from loop

List<List<integer>> mainlist = new List<List<integer>>
{
    new List<integer>{1, 1, 9},
    new List<integer>{1, 2, 0},
    new List<integer>{1, 3, 6},
    new List<integer>{1, 4, 5, 2}
};
    
    system.debug('mainlist'+ mainlist);

   for(integer i=0;i<mainlist.size();i++)
   {
       system.debug('==>>'+mainlist.get(i));// with tihs i can loop all ((1, 1, 9), (1, 2, 0), (1, 3, 6), (1, 4, 5, 2))
       for(integer j)
                      // how to wirte code to display indivisual elements 
   }

Thanks
 
Best Answer chosen by Vijay@sfdc
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
List<List<integer>> mainlist = new List<List<integer>>
{
    new List<integer>{1, 1, 9},
    new List<integer>{1, 2, 0},
    new List<integer>{1, 3, 6},
    new List<integer>{1, 4, 5, 2}
};
    
    system.debug('mainlist'+ mainlist);

   for(integer i=0;i<mainlist.size();i++)
   {
       system.debug('==>>'+mainlist.get(i));

       List<integer> innerList = mainlist.get(i);
       For(integer n : innerList )
      {
        System.debug(i+'------>'+n);
      }
   }
Output will like below
 
12:33:23.217 (217522983)|EXECUTION_STARTED
12:33:23.217 (217551209)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
12:33:23.217 (218696596)|USER_DEBUG|[9]|DEBUG|mainlist((1, 1, 9), (1, 2, 0), (1, 3, 6), (1, 4, 5, 2))
12:33:23.217 (218817722)|USER_DEBUG|[13]|DEBUG|==>>(1, 1, 9)
12:33:23.217 (219018964)|USER_DEBUG|[18]|DEBUG|0------>1
12:33:23.217 (219071044)|USER_DEBUG|[18]|DEBUG|0------>1
12:33:23.217 (219119096)|USER_DEBUG|[18]|DEBUG|0------>9
12:33:23.217 (219203984)|USER_DEBUG|[13]|DEBUG|==>>(1, 2, 0)
12:33:23.217 (219267452)|USER_DEBUG|[18]|DEBUG|1------>1
12:33:23.217 (219318415)|USER_DEBUG|[18]|DEBUG|1------>2
12:33:23.217 (219389241)|USER_DEBUG|[18]|DEBUG|1------>0
12:33:23.217 (219449573)|USER_DEBUG|[13]|DEBUG|==>>(1, 3, 6)
12:33:23.217 (219537614)|USER_DEBUG|[18]|DEBUG|2------>1
12:33:23.217 (219627164)|USER_DEBUG|[18]|DEBUG|2------>3
12:33:23.217 (219675184)|USER_DEBUG|[18]|DEBUG|2------>6
12:33:23.217 (219730210)|USER_DEBUG|[13]|DEBUG|==>>(1, 4, 5, 2)
12:33:23.217 (219789809)|USER_DEBUG|[18]|DEBUG|3------>1
12:33:23.217 (219833953)|USER_DEBUG|[18]|DEBUG|3------>4
12:33:23.217 (219877145)|USER_DEBUG|[18]|DEBUG|3------>5
12:33:23.217 (219919632)|USER_DEBUG|[18]|DEBUG|3------>2
12:33:23.217 (219986910)|CODE_UNIT_FINISHED|execute_anonymous_apex
12:33:23.217 (221475172)|EXECUTION_FINISHED
Let us know if this will help u

Thanks
Amit Chaudhary


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
List<List<integer>> mainlist = new List<List<integer>>
{
    new List<integer>{1, 1, 9},
    new List<integer>{1, 2, 0},
    new List<integer>{1, 3, 6},
    new List<integer>{1, 4, 5, 2}
};
    
    system.debug('mainlist'+ mainlist);

   for(integer i=0;i<mainlist.size();i++)
   {
       system.debug('==>>'+mainlist.get(i));

       List<integer> innerList = mainlist.get(i);
       For(integer n : innerList )
      {
        System.debug(i+'------>'+n);
      }
   }
Output will like below
 
12:33:23.217 (217522983)|EXECUTION_STARTED
12:33:23.217 (217551209)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
12:33:23.217 (218696596)|USER_DEBUG|[9]|DEBUG|mainlist((1, 1, 9), (1, 2, 0), (1, 3, 6), (1, 4, 5, 2))
12:33:23.217 (218817722)|USER_DEBUG|[13]|DEBUG|==>>(1, 1, 9)
12:33:23.217 (219018964)|USER_DEBUG|[18]|DEBUG|0------>1
12:33:23.217 (219071044)|USER_DEBUG|[18]|DEBUG|0------>1
12:33:23.217 (219119096)|USER_DEBUG|[18]|DEBUG|0------>9
12:33:23.217 (219203984)|USER_DEBUG|[13]|DEBUG|==>>(1, 2, 0)
12:33:23.217 (219267452)|USER_DEBUG|[18]|DEBUG|1------>1
12:33:23.217 (219318415)|USER_DEBUG|[18]|DEBUG|1------>2
12:33:23.217 (219389241)|USER_DEBUG|[18]|DEBUG|1------>0
12:33:23.217 (219449573)|USER_DEBUG|[13]|DEBUG|==>>(1, 3, 6)
12:33:23.217 (219537614)|USER_DEBUG|[18]|DEBUG|2------>1
12:33:23.217 (219627164)|USER_DEBUG|[18]|DEBUG|2------>3
12:33:23.217 (219675184)|USER_DEBUG|[18]|DEBUG|2------>6
12:33:23.217 (219730210)|USER_DEBUG|[13]|DEBUG|==>>(1, 4, 5, 2)
12:33:23.217 (219789809)|USER_DEBUG|[18]|DEBUG|3------>1
12:33:23.217 (219833953)|USER_DEBUG|[18]|DEBUG|3------>4
12:33:23.217 (219877145)|USER_DEBUG|[18]|DEBUG|3------>5
12:33:23.217 (219919632)|USER_DEBUG|[18]|DEBUG|3------>2
12:33:23.217 (219986910)|CODE_UNIT_FINISHED|execute_anonymous_apex
12:33:23.217 (221475172)|EXECUTION_FINISHED
Let us know if this will help u

Thanks
Amit Chaudhary


 
This was selected as the best answer
Vijay@sfdcVijay@sfdc
Thanks Amit :)