0

i am getting an error given below while i trying to iterate a List of String lists. I am thinking that i missed something very simple but did not able to move beyond this issue. Please clarify.

Save error: Loop variable must be of type LIST

List<List<String>> lstResp = new List<List<String>> {};
lstResp = Utility.ParseMultipleResponse(ret);
for(List<List<String>> n: lstResp) { //error line 
   for(List<String> l: n) {
     //-------some code here-------------
   }
}
9

Hopefully these names help explain what is going on:

List<List<String>> listOfListOfString = Utility.ParseMultipleResponse(ret);
for (List<String> listOfString : listOfListOfString) {
    for (String str : listOfString) {
        // Code
    }
}

Each for loop iterates over a list, so the type of the for loop variable is "one list less" in each case.

1
  • Thanks @Keith. I understood the issue now based on your explanation. – Bforce May 5 '14 at 17:43
3

Your list is type of List<String>. So an element is type of that. Your for loop should be something like,

for(List<String> firstList: lstResp) { 
   for(String singleString : firstList){
   // Some code
   }
}

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.