1

I'm currently studying Linked List and on Iterators(using Lafore's).

I am not sure as to why we need Iterators in the first place.

Say your class Link looks like below.

class Link{
public int iData; // data
public double dData; /d/ data
public Link next; //reference to next link
//----------------------------------
public Link(int id, double dd){
    iData = id; //initialize data
    dData = dd;// 'next' is automatically set to null.
}
//----------------------------------
public void displayLink(){
    System.out.print("{"+iData+", "+dData+"} ");
}
//----------------------------------
}//end class Link

And your LinkList class has a field Link first and some operations for the list.

My question is

Why can't I just add an int variable(ex: int count;) as a field for the Link class and then just increment it whenever it's called instead of building a whole class ,called Iterators, just to traverse back and forth in the list?

The book says

One problem with this approach is that you might need more than one such reference, just as you often use several array indices at the same time.

What does it mean? Can you give me an example of when this problem occurs?

Thank you in advance.

6
  • There is inbuilt arraylist in java why not take some look at it you can do all operations inserting at specified position and dynamic too. Commented Feb 28, 2016 at 15:01
  • Of course you can do that. Maybe a better question is: What is the motivation for the Iterator pattern? Patterns are solutions to common problems in object-oriented programming. Here's the motivation for Iterator: sourcemaking.com/design_patterns/iterator Commented Feb 28, 2016 at 15:01
  • You probably want a look at this to appreciate iterator pattern. Commented Feb 28, 2016 at 15:01
  • 1
    I don't understand what you mean with this int field. What would it do? What do you mean by "whenever it's called"? You don't "call" a class. The idea of an iterator is to be able to iterate over the collection without caring whether it's a linked list, an array list or a hash set: you use its iterator, and you iterate the same way, with optimal performance. Commented Feb 28, 2016 at 15:02
  • 1
    As an example of using more than one references, suppose you wanted to iterate the Link objects in ascending order of iData, but also in ascending order of dData (not at the same time, but in different situations). You could use two different references to next link: public Link next_by_idata; and public Link next_by_ddata;. It would be a little more complicated to manage, but would allow accessing either way very rapidly. Commented Feb 28, 2016 at 15:09

1 Answer 1

2

If you add a count field in Link, then if you insert an element somewhere in the middle, you would have to update the .count field in all subsequent elements. That would make insert operations inefficient.

And what use would be a Link.count field? I don't see a generally useful purpose for that.

An iterator is nice, but you don't actually need one. If you want to traverse a linked list, you can certainly do without it. So I really don't see your objection against iterators on linked lists.

Iterators are useful as they encapsulate the concept of iterating. The single responsibility of an iterator is that it iterates over some iterable. You can iterate over the elements without having to know anything about the underlying storage.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.