1

I was reading the book algorithms by Robert Sedgewick and I came across chapter 1.3 about ADTs and I have a few questions.

Are all ADT's in java iterable?

Does that mean we have to implement an iterator each time we implement an ADT?

If so, do I make a separate API for the iterator? Because in page 141 he made a interface just for the Iterator.

I was talking to a friend and he said "An instance of an ADT can be modified (i.e. elements added/deleted/or modified) when iterated" isn't this wrong because in a bag elements cannot be removed?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Red Eyez
  • 187
  • 3
  • 16
  • 1
    I removed the unrelated question from your post, as we prefer to have one question per post here. Feel free to ask that in a separate question post (you can find the old version in the [revision history](https://stackoverflow.com/posts/52333409/revisions)). Although it is a bit hard to understand what you were trying to ask there. – Bernhard Barker Sep 14 '18 at 14:01
  • well in the book he mentions that iterators are important and how they help us in prcessing data. but ADTs can be implemented by anyone does this mean we have to implement the iterator as well? – Red Eyez Sep 14 '18 at 14:10
  • You might want to read [What is an abstract data type in object oriented programming?](https://stackoverflow.com/questions/1692933/what-is-an-abstract-data-type-in-object-oriented-programming) I'm inclined to say all your mentions of "ADT" should be replaced with "class". – Bernhard Barker Sep 14 '18 at 14:11
  • i was thinking more about stacks, bags and queues, which have iterators. but suppose that someone implemented a similar thing manually. thats why i used ADT – Red Eyez Sep 14 '18 at 14:17

1 Answers1

4

An abstract data type is nothing more than a set of operations together with the contract for these operations. Typically, there are several implementations of an ADT possible that may show different non-functional characteristics (e.g. run time of operations) or different bahaviour in areas that are not specified in the contract. It depends on the nature of the concrete ADT if iteration is one of the operations. It does not have to.

In Java, you can only define the signatures of the operations (e.g. in an interface). The contract has to be given informally for example in the Javadoc.

Henry
  • 42,982
  • 7
  • 68
  • 84