I have 12 errors in my code and I just have no idea how to do this one
Be sure that the getIterator method returns an anonymous class.
Transfer the LocalIterator logic to an anonymous class.
Only change the body of the getIterator method.
Requirements:
The getIterator method must return an Iterator object.
The object returned by the getIterator method must be an instance of an anonymous class.
The program should display data on the screen.
The behavior of the program should remain the same.
....
package com.codegym.task.task24.task2410;
public interface Iterator {
Iterator next();
}
A few things:
#1 The LocalIterator class was a class within the method. Once you remove it (per the task instructions) it no longer exists. You won't be able to return an anonymous class of a Local Iterator because of this
#2 It's good that you just commented out the code because that commented code is what should be in the anonymous class
#2a Anonymous classes can't have constructors, which will cause an error when you copy and paste the constructor in. Here is a link from stackoverflow that should guide you on how to solve that problem:
anonymous class constructor?
Interesting side note. This task is mimicking how an Iterator is actually formed. I am talking about the iterators that are used to iterate through a collection. An iterator is only an interface. When you call the .iterator() method on a collection it returns an anonymous class that implements the interface. This provides a standard way of traversing a collection that doesn't require a bunch of special code to handle the way that the specific collection stores and retrieves its data. For example, with ArrayLists you can retrieve an item by its index number, but with HashMaps index numbers have no meaning and instead you retrieve values with keys. With HashSets you can't even retrieve the values with just the Sets methods and have to use an iterator to do such.
I forgot to mention that you can't return a new LocalIterator within the next() method. It shouldn't be too hard to intuit what you should return (and it isn't wirting code for an additional anonymous class). I'll give you a hint. The code inside the anonymous class has access to all the code within the class that it comes from: methods, fields, public to private.
still failing obviously.
I'm still completely lost with this task, why and how to create this anonymous class, and how to return it.
I also can't tell what the iterator next() method is doing.
In the public Iterator getIterator(final String name) method we create an Iterator class and then return it at the end of the method. So why do we need a method inside the inner class itself to return the Object (apart from having to implement the interface's methods)?