Please check the instructions you were given carefully; it is usual for the Iterator to be an inner class, so you don't need separate top‑level classes. That would also allow users to create multiple Iterator objects, and that would breach this part of the contract for Iterator:-
Luke Stone wrote:Sorry for the confusion, to clear up a few things:
This is for school. We have been making these iterator classes to test with code given to us by my instructor.
If you have multiple Iterators, it is possible for iterator1 to remove or add elements while iterator2 is iterating in which case the next element might be incorrect.
. . . Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. . . .
The instanceof operator is usually a sign of poor object‑oriented design. You do however usually need instanceof in the second part of an equals() method.
We are trying to implement the instanceof in this method
Oh yes, you are. You are overriding the equals() method. You are doing it in such a way that equals() alone will work, but any attempt to put your Lists into hash‑based collections will fail miserably. The rules for equals(), which I gave you a link to last night, tell you clearly that you must override hashCode() too.
We are not really overriding anything
Beware of looking for programming advice on Google; there is some good advice and some bad advice out there, and Google doesn't know the difference. Work out from the links I gave you where the API docuementation is, and bookmark that link in your browser. When you can't understand something, ask us.
I'm not sure what the hasCode() method is. I googled it and we have not needed to use it yet.
The equals() method must accept any kind of object as its parameter. You have eliminated everything which isn't a List in line 19, So, whatever reaches line 22 must be a List. So far, so good. But how do you know what the List contains? What if you are in a List<String> and somebody tries to test for equality against a List<Integer> or a List<Car> or a List<Foo>? I suspect you are going to get problems when you try to create your Iterator<String>. The runtime will try to cast things to Strings, which they aren't. Remember every cast is defiance to the javac tool; it means, “I know the cast will work without any problems.” Try casting to (List) not (List<E>). Or (List<?>) because you don't know what sort of element the List will contain. The <?> means you can get elements out of the List and not add elements, but that doesn't matter; you don't want to add or remove anything in an equals() method.
When you mentioned Line 22, what do you mean by "Cast your parameter to List full stop." ??
It is more than that; it is a nice simple way to fill a List with 1,000,000 different String objects.
. . .This just shows how many stuff is in the list when printed out.
Why are you creating head and tail nodes in the constructor? A linked list usually has exactly the same number of nodes as it has elements, so you would create your first node in the add() method.
Then in another method thats where he tests to see if they are equal in many ways. adds different things to the list and so forth.
but any attempt to put your Lists into hash‑based collections will fail miserably. The rules for equals(), which I gave you a link to last night, tell you clearly that you must override hashCode() too.
But along with a linear traversal of a linked list, you might have to do a linear traversal of the array list, and that also runs in O(n) complexity. So we are stuck with that sort of performance. Of course, if you use an index to traverse an array list, you are still going to be in linear time, whereas the linked list may go into O(n²) complexity.
Junilu Lacar wrote:. . . a linked list . . . its time complexity is O(n), . . . .
That may be caused by iffy design of the other classes.
Junilu Lacar wrote:. . . your implementation of equals() is oddly specific. . . .
Campbell Ritchie wrote:If you try this sort of thing, however, then you go into quadratic time:-If you try that with an array list, you run in linear time.