Мне нужно реализовать Iterator, который должен внутренне перебирать несколько частных итераторов на разных уровнях. Например, Iterator, Iterator и Iterator. Iterator существует на протяжении всего срока службы моего Iterator, но экземпляры Iterator и Iterator создаются из текущих значений для A и B соответственно.
На любой данной итерации Iterator может быть исчерпан, поэтому мне придется перейти к next() Iterator, из которого я создам новый Iterator. Надеюсь, это понятно.
Мой вопрос: есть ли структура лучше, чем эта (ниже)? В моем реальном случае есть 5-6 уровней вложенных итераторов, и я нахожу это уродливым и неудовлетворительным.
if ( this.cIterator == null || ! this.cIterator.hasNext() ) {
// We do not have a C iterator established, or the one we had established is exhausted
// We need a new one, which will come from the next B
if ( this.bIterator = null || !this.bIterator.hasNext() ) {
// We do not have a B iterator established or the one we had is exhausted.
// We need a new one, which will come from the next A
if ( this.aIterator == null || !this.aIterator.hasNext() ) {
// Our aIterator is non-existent or exhausted. We are done.
throw new NoSuchElementException();
}
// At this point, we can move on to the next A.
this.currentA = this.aIterator.next();
this.bIterator = this.currentA.getIteratorSourceCollection().iterator();
// assert this.bIterator.hasNext(); // will be true in my case
}
// At this point, we are guaranteed to have a B iterator with at least one element left
this.currentB = bIterator.next();
this.cIterator = this.currentB.getIteratorSourceCollection().iterator();
// assert this.cIterator.hasNext(); // will be true in my case
}
// At this point, we are guaranteed to have a C iterator with a next element.
return new E(this.currentA, this.currentB, this.cIterator.next());
Подробнее здесь: https://stackoverflow.com/questions/798 ... -iterators
Перебор нескольких вложенных итераторов ⇐ JAVA
Программисты JAVA общаются здесь
1762822680
Anonymous
Мне нужно реализовать Iterator, который должен внутренне перебирать несколько частных итераторов на разных уровнях. Например, Iterator, Iterator и Iterator. Iterator существует на протяжении всего срока службы моего Iterator, но экземпляры Iterator и Iterator создаются из текущих значений для A и B соответственно.
На любой данной итерации Iterator может быть исчерпан, поэтому мне придется перейти к next() Iterator, из которого я создам новый Iterator. Надеюсь, это понятно.
[b]Мой вопрос: есть ли структура лучше, чем эта (ниже)?[/b] В моем реальном случае есть 5-6 уровней вложенных итераторов, и я нахожу это уродливым и неудовлетворительным.
if ( this.cIterator == null || ! this.cIterator.hasNext() ) {
// We do not have a C iterator established, or the one we had established is exhausted
// We need a new one, which will come from the next B
if ( this.bIterator = null || !this.bIterator.hasNext() ) {
// We do not have a B iterator established or the one we had is exhausted.
// We need a new one, which will come from the next A
if ( this.aIterator == null || !this.aIterator.hasNext() ) {
// Our aIterator is non-existent or exhausted. We are done.
throw new NoSuchElementException();
}
// At this point, we can move on to the next A.
this.currentA = this.aIterator.next();
this.bIterator = this.currentA.getIteratorSourceCollection().iterator();
// assert this.bIterator.hasNext(); // will be true in my case
}
// At this point, we are guaranteed to have a B iterator with at least one element left
this.currentB = bIterator.next();
this.cIterator = this.currentB.getIteratorSourceCollection().iterator();
// assert this.cIterator.hasNext(); // will be true in my case
}
// At this point, we are guaranteed to have a C iterator with a next element.
return new E(this.currentA, this.currentB, this.cIterator.next());
Подробнее здесь: [url]https://stackoverflow.com/questions/79816153/iterating-over-several-nested-iterators[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия