4
public with sharing class CustomListIterator

{

        list<integer>num = new list<integer>{1,2,3,4,5,6};

        iterator numIterator = num.iterator();

        while(numIterator.hasNext())

        {

             system.debug(numIterator.next());

        }
}

This Code gave an error and not save.

Error: Compile Error: expecting right curly bracket, found 'while' at line 5 column 4

  • what error you are getting? – Tushar Sharma Mar 29 '16 at 9:32
  • Error: Compile Error: expecting right curly bracket, found 'while' at line 5 column 4 – Bonny Mar 29 '16 at 9:33
  • change this iterator numIterator = num.iterator(); into iterator<Integer> numIterator = num.iterator(); – Tushar Sharma Mar 29 '16 at 10:11
  • and this is complete class code? – Tushar Sharma Mar 29 '16 at 10:11
3

Write your code inside a method inorder to avoid that error.You are directly wrote the code inside class.

public with sharing class CustomListIterator{

  public void method_name(){

    list<integer>num = new list<integer>{1,2,3,4,5,6};

        iterator<integer> numIterator = num.iterator();

        while(numIterator.hasNext())

        {
             system.debug(numIterator.next());
        }
  }

}
| improve this answer | |
3

enter image description hereYou need to mention type for iterator.

List<integer> num = new List<integer>{1,2,3,4,5,6};
Iterator<Integer> numIterator = num.iterator();

while(numIterator.hasNext()){
   system.debug(numIterator.next());
}

This is working now. Hope this helps.

Added screenshot.

| improve this answer | |
  • Thanks Kiran.But already tried this gave same error. – Bonny Mar 29 '16 at 9:39

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.