So my teacher told us that we had to write a method called iterator() in a class called Dictionary. Dictionary has one private instance variable (and may only have that one instance variable) that is an ArrayList of strings. Dictionary implements Iterable<E>. I made my class name Dictionary<E> because I was under the impression that that was the only way to implement Iterable<E>. My problem is that the method iterator is supposed to provide a java.util.Iterator that iterates over the Dictionary in order, a word at a time, from beginning to end. First of all what the hell does that mean? Second of all when I tried to use ArrayList's method iterator to return an iterator of my arrayList, it won't let me because my teacher told me my instance variable arraylist had to be of strings, and that my iterator method had to return a java.util.iterator<String>. However, my iterator method will only return an iterator of type E, and so I get an Error stating:
found : java.util.Iterator<java.lang.String>
required: java.util.Iterator<E>
Here's my code, please try to help I am at a point of staring at code.
import java.util.ArrayList; import java.util.Scanner; public class Dictionary<E> implements Iterable<E> { private ArrayList<String> list; public Dictionary() {} public Dictionary(String fileName) { Scanner sc = new Scanner(fileName); while(sc.hasNext() == true) { list.add(sc.next()); } } public java.util.Iterator<String> iterator() { return list.iterator(); } }