Trying to implement my own iterator in Java

  • Comp Sci
  • Thread starter trouty323
  • Start date
  • Tags
    Java
In summary, the student is struggling with implementing their own iterator using a Doubly Linked List and is specifically having trouble with the remove() method. They are asking for clarification on whether their current code is correct and are open to suggestions for improvement. An example solution for the remove() method is provided.
  • #1
trouty323
24
0

Homework Statement


Basically, I am assigned to implement my own iterator using a Doubly Linked List. The methods, of course, are hasNext(), next(), and remove(). The one that is giving me the most grief is the remove() method.


Homework Equations





The Attempt at a Solution



The code that I have currently is:

Code:
public void remove() {
	lastItemReturned = lastItemReturned.prev;
	nextItem = lastItemReturned.next.next;
	index--;
}

Now, this could actually be right and I may be messing up somewhere else in the code, but I have a feeling this is not the case. Does anybody know if this part of code is right?
 
Physics news on Phys.org
  • #2


Dear student,

Thank you for reaching out for help with your iterator implementation. The remove() method in your code looks like it may be incorrect. It is important to remember that when removing an item from a linked list, you need to update the pointers of the previous and next nodes in order to maintain the integrity of the list.

Here is a possible solution for your remove() method:

public void remove() {
if(lastItemReturned == null) {
throw new IllegalStateException("No item to remove.");
}
// check if lastItemReturned is the first node
if(lastItemReturned.prev == null) {
// update the next node's prev pointer
lastItemReturned.next.prev = null;
// update the nextItem pointer
nextItem = lastItemReturned.next;
} else {
// update the previous node's next pointer
lastItemReturned.prev.next = lastItemReturned.next;
// update the next node's prev pointer
lastItemReturned.next.prev = lastItemReturned.prev;
// update the nextItem pointer
nextItem = lastItemReturned.next;
}
// update the index
index--;
// set lastItemReturned to null
lastItemReturned = null;
}

I hope this helps you with your implementation. Remember to thoroughly test your code and consider edge cases to ensure it is functioning correctly. Good luck!
 

What is an iterator in Java?

An iterator in Java is an object that allows you to traverse and access the elements of a collection sequentially, without the need for an index. It provides methods for checking if there are more elements in the collection, retrieving the next element, and removing elements from the collection.

Why would I want to implement my own iterator?

Implementing your own iterator allows you to define the specific behavior and logic for traversing and accessing elements in a collection. This can be useful if you have a custom data structure or if you want to optimize the iteration process for your specific needs.

What are the basic steps for implementing an iterator in Java?

The basic steps for implementing an iterator in Java are: creating a class that implements the Iterator interface, defining the necessary methods such as hasNext() and next(), and then using the iterator to traverse the collection in your code.

How do I use my custom iterator in my code?

Once you have implemented your own iterator, you can use it by creating an instance of your iterator class and then calling its methods, such as hasNext() and next(), to iterate through the elements of your collection.

Are there any built-in iterators in Java?

Yes, Java provides built-in iterators for its collection classes such as ArrayList and LinkedList. These iterators can be accessed through the iterator() method of the collection class.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
986
  • Engineering and Comp Sci Homework Help
Replies
1
Views
882
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top