Skip to content
Advertisement

Can’t access my linked list methods and can’t iterate trough it

So im following along this playlist about data structures and in this video to conclude the linked list part, the professor explain we need an inner class called IteratorHelper.

Video: https://www.youtube.com/watch?v=bx0ebSGUKto&list=PLpPXw4zFa0uKKhaSz87IowJnOTzh9tiBk&index=21

This is the code in my github with the linked list implementation and the main class called tester: https://github.com/Ghevi/Algos-DataStructures/tree/master/src/com/ghevi/ads/linkedlists

The problem is that the tester class can’t compile. If I instantiate the linked list as an ListIterator i can’t access its methods. I also can’t iterate trough it regardless of having the IteratorHelper inner class. In the video he writes “implements ListI<>” is just a shorter version for ListIterator<>? Sorry im just a beginner.

JavaScript

Advertisement

Answer

The video is not very clear, but basically LinkedList should implement Iterable, not ListIterator. IteratorHelper should implement ListIterator (see 4:20 timestamp).

Here’s the fixed code:

JavaScript

The interface methods hasPrevious, next, etc… have been moved into the IteratorHelper class which implements Iterator. The LinkedList class has an iterator() method because it implements Iterable. Now you can instantiate a LinkedList object and iterate over it in a for-loop:

JavaScript

Here’s a handy chart to remind you which class should have which functions: enter image description here

More on Iterable vs Iterator

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement