0

When create a linkedlist in hash class search method, my iterator does not work with it. What can i use instead of iterator? How can i fix it.

public static void search(String keyword){
    String hashString = hash(new String(keyword.toLowerCase()));
    if (maps.containsKey(hashString)) {
        LinkedList<String> list = maps.get(hashString);

        Iterator iter = list.iterator();
        System.out.println("This word has been appeared in following text files: ");
        while (iter.hasNext()) {
            System.out.print(iter.next() + "");
            if (iter.hasNext()) {
                System.out.print(", ");
            }
        }
        System.out.println("");
    } else {
        System.out.println("No text files includes this word.");
    }
    System.out.println("");
}

package hash;

public class LinkedList<Item> {
Node<Item> first;
Node<Item> last;
int size = 0;

public LinkedList(){
    first = null;
    last = null;
    size = 0;
}
}
3
  • This would work fine with the built in linked list. Why are you building your own, and if you are, why aren't you implementing an iterator? Commented Dec 20, 2021 at 19:53
  • I use iterator to find words in txt files but it throws an error. How can I solve this problem? Commented Dec 20, 2021 at 19:56
  • 2
    The iterator() method would only exist if you defined it in your class. Since you are defining your own LinkedList class (I assume as coursework since there's already a robust LinkedList class in the standard library), you have to write your own implementation of the Iterator class and return an instance of it in a method of your LinkedList class. Commented Dec 20, 2021 at 19:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.