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
Andrew GrayAndrew Gray 

Nested List help

Am struggling with nested lists. I want to create one list (list2) with all the string values from the nested lists in list1. How do I assign in the loop ?
 
public static void ctask1(List<List<String>> list1){

		List<String> list2 = new List<String>();

        for(List<String> l: list1) {          
            //?                 
        }

}


Thanks

 
Best Answer chosen by Andrew Gray
Jaya Karthik  karnatiJaya Karthik karnati
Hi Andrew,

For getting values for nested loop , we need to use nested for loop.
 
public static void ctask1(List<List<String>> list1){

		List<String> list2 = new List<String>();

        for(List<String> l: list1) {          
              for(string str:l) {
                    list2.add(str);
               }                
        }

}

Hope it helps. if so kindly mark it as best answer.

Thanks,
karthik