почему iterator.remove () был описан как дополнительная операция?

Указатели (т.е. '* ') должны использоваться, где передающий "ПУСТОЙ УКАЗАТЕЛЬ" значим. Например, Вы могли бы использовать ПУСТОЙ УКАЗАТЕЛЬ для представления этого должен быть создан, конкретный объект, или что конкретные меры не должны быть приняты. Или если это когда-нибудь нужно называть от кода НеC++. (например, для использования в общих библиотеках)

, например, функция libc time_t time (time_t *result);

, Если result не является ПУСТЫМ, будет сохранено текущее время. Но если result является ПУСТЫМ, то никакие меры не приняты.

, Если функция, которую Вы пишете, не должна использовать ПУСТОЙ УКАЗАТЕЛЬ в качестве значимого значения затем с помощью ссылок (т.е. '&';) будет, вероятно, менее сбивать с толку - предполагающий, что это - конвенция, которую использует Ваш проект.

9
задан Mogsdad 17 January 2018 в 03:43
поделиться

3 ответа

#1: Optional means you can implement it or throw an UnsupportedOperationException

#2: This operation is optional because sometimes you just don't want your iterator's content to be modified. Or what do you understand by "robustness of operation"?

EDIT #4: behavior of an iterator is unspecified if the underlying collection is modified

Normally, you use an iterator by executing

List<String> c = new ArrayList<String>();
c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
...
for (Iterator<String> i = c.iterator(); i.hasNext();)
{
  String s = i.next();
  ...
}

If you now would want to remove an item while iterating through the list, and you would call

c.remove("Item 2");

this is not clean, possibly corrupts data in your List/Collection/... and should be avoided. Instead, remove() the item through the iterator:

i.remove();
13
ответ дан 4 December 2019 в 10:05
поделиться

First of all java.util.Iterator is an interface i.e. an agreement how classes that implement this interface interract with the rest of the world. It's their responsibility how they'll implement interaface's methods.

If the underlying data structure doesn't allow removal then remove() will throw an UnsupportedOperationException. For example, if you are iterating through a result set retrieved from a DB it does make sense not to implement this method.

If you iterate over some collection which is shared between concurrent threads and the other thread modifies the data iterating thread then will return undeterministic results.

6
ответ дан 4 December 2019 в 10:05
поделиться

It is described as being optional because not all collection classes that can give you an iterator implement the remove() method in the iterator they return. If the returned iterator doesn't implement it, an UnsupportedOperationException will be thrown.

The normal java.util.ArrayList, java.util.LinkedList and other standard collection classes all implement the remove() method in their iterators, so you can use it safely.

2
ответ дан 4 December 2019 в 10:05
поделиться
Другие вопросы по тегам:

Похожие вопросы: