Creating a DoublyLinkedList Iterator in Java: Tips and Tricks

  • Comp Sci
  • Thread starter Live4eva_2
  • Start date
  • Tags
    Java Tips
In summary, the speaker is struggling to create a DoublyLinkedList<T> class in Java and is worried about the iterator. They have read tutorials and documents that use three classes for this, but the speaker wonders why they can't simply include the iterator reference variables in the DoublyLinkedList<T> class. They also have another question about visibility when compiling and have asked for help on a forum.
  • #1
Live4eva_2
28
0
Hi I'm struggling to create a DoublyLinkedList<T> class in Java.
Well,it's actually the iterator that's freaking me out..
Most tutorials and documents I've read use three classes for this:Node<T>,DoublyLinkedList<T> and LinkedListIterator<T>.

I can see the point in creating Node and DoublyLinkedList<T> classes but i don't know why I can't simply just include the 2 iterator reference variables(next and previous) in the DoublyLinkedList<T> class instead of writing an additional iterator class.
 
Physics news on Phys.org
  • #2
OK nevermind that..Let me pose another question:
public class LinkedList<T>
{
public Node<T> first;
public Node<T> last;
protected class Node<T>
{
public Node<T> next;
public Node<T> last;


}
public class Iterator<T>
{
protected Node<T> current;
protected Node<T> previous;


}


}
I've only included the classes and variables in order to illustrate the visibility.
when I complile I'm getting the msg that "Node<T> current" isn't available in LinkedList<T> class...why not !?Am I only allowed one inner class?Also i ensured that my iterator was constructed within the LinkedList constructor so i don't think that it could be a problem with instantiating the iterator object.
 
  • #3

Related to Creating a DoublyLinkedList Iterator in Java: Tips and Tricks

1. What is a LinkedList Iterator in Java?

A LinkedList Iterator in Java is an object that allows you to iterate through the elements in a LinkedList in a sequential manner. It is used to traverse the elements in the list, retrieve or remove specific elements, and perform other operations on the list.

2. How do I create a LinkedList Iterator in Java?

To create a LinkedList Iterator in Java, you need to first create a LinkedList object and then use the iterator() method to get the iterator for that list. You can then use the iterator to traverse through the elements in the list.

3. How do I use a LinkedList Iterator to iterate through a LinkedList?

To use a LinkedList Iterator to iterate through a LinkedList, you can use a while loop or a for loop. Within the loop, you can use the hasNext() and next() methods to check for the next element and retrieve it respectively.

4. Can I remove elements while using a LinkedList Iterator in Java?

Yes, you can remove elements while using a LinkedList Iterator in Java. The remove() method of the iterator can be used to remove the current element from the list without disrupting the iteration process.

5. What are some tips for using a LinkedList Iterator in Java?

Some tips for using a LinkedList Iterator in Java include remembering to use the hasNext() method before calling next(), using the remove() method carefully to avoid ConcurrentModificationException, and understanding the difference between Iterator and ListIterator for navigating a LinkedList.

Similar threads

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