KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Inspired in this question: How to implements Iterable I decided to make a basic linked list implementation and implement an iterator in order to have a code like this: MyList<String> myList = new MyList<String>(); myList.add("hello"); myList.add("world"); for(String s : myList) { System.out.println(s); } The code wasn't hard to deal with, creating a class MyList<T> implements Iterable<T> with a private static class Node<T> and a private class MyListIterator<T> implements Iterator<T> , but then I came across a problem when implementing my own version of Iterator#remove : class MyList<T> implements Iterable<T> { private static class Node<T> { //basic node implementation... } private Node<T> head; private Node<T> tail; //constructor, add methods... private class MyListIterator<T> implements Iterator<T> { private Node<T> headItr; private Node<T> prevItr; public MyListIterator(Node<T> headItr) { this.headItr = headItr; } @Override public void remove() { //line below compiles if (head == headItr) { //line below compiles head = head.getNext(); //line below doesn't and gives me the message //"Type mismatch: cannot convert from another.main.MyList.Node<T> to //another.main.MyList.Node<T>" head = headItr.getNext(); //line below doesn't compile, just for testing purposes (it will be deleted) head = headItr; } } } } This error message raised my curiosity. I was looking on the net about this problem but found nothing (or pr
Tags (comma-separated)
Save Edits
Cancel