It is a very simple collection program but not running

advertisements

I am using latest jdk to run this program. i cant find the right solution here pls help.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

class ArrayListDemo{
    List<String> list = new ArrayList<>();
    list.add("abc");
    list.add("xyz");
    Iterator<String> itr = list.iterator();
    while(itr.){
            System.out.println(itr.next());
    }
}


Seems to be a compilation error due to two things:

The contents of your class should actually be inside a method, not the class body itself.

The line with while (itr.){ should read while (itr.hasNext ()){