0
$\begingroup$

Whenever a caller or client modifies a library collection class, all iterators are made invalid (with the exception of the iterator through which the change was made, in the case of remove). Give a reason for this rule in the case of an array list (using a dynamically sized array, in which an iterator has a pointer to the array) and in the case of list (using linked list nodes, in which case the iterator points to one or more list nodes).

$\endgroup$

closed as off-topic by xskxzr, Juho, Evil, David Richerby, hengxin Dec 22 '18 at 4:01

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – xskxzr, Evil, David Richerby, hengxin
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Java-specific questions are off-topic here. But this question seems more general. $\endgroup$ – Yuval Filmus Dec 16 '18 at 0:35
  • $\begingroup$ Your newest edit seemed to completely change the question. Please ask a new question instead. $\endgroup$ – Juho Dec 16 '18 at 10:05
1
$\begingroup$

The question is "why".

There are two problems: First, you would have to define how an iterator that doesn't become invalid would change if the underlying container changes. That will be an interesting problem. Some languages have iterators that can iterate through hash tables, enumerating all (key, value) pairs in some order. Now imagine you insert an item into the hash table, the number of items becomes too large, and the whole hash table is re-hashed, and you need to define how the iterator is modified in a meaningful way.

Second, if the iterators need to stay valid, you need to keep track somehow which iterators are involved with which underlying container. That's not too difficult, but costs time and space. Together with the first problem, it's much easier to just declare any iterator as undefined after a change.

PS. If you copy / paste this as an answer to a homework question, your teacher will most likely trace it back to this post.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.