SoldiersIterator iterator = new SoldiersIterator(earthArmy);
while (iterator.hasNext()){
Soldier currSoldier = iterator.next();
currSoldier.treat();
}
* This source code was highlighted with Source Code Highlighter .
Army earthArmy = new Army();
Group groupA = new Group();
for ( int i=1; i<4; ++i)
groupA.addNewSoldier( new Soldier( "Alpha:" + i));
Group groupB = new Group();
for ( int i=1; i<3; ++i)
groupB.addNewSoldier( new Soldier( "Beta:" + i));
Group groupC = new Group();
for ( int i=1; i<2; ++i)
groupC.addNewSoldier( new Soldier( "Gamma:" + i));
earthArmy.ArmyHero = new Hero( "Andriy Buday" );
earthArmy.addArmyGroup(groupB);
earthArmy.addArmyGroup(groupA);
earthArmy.addArmyGroup(groupC);
* This source code was highlighted with Source Code Highlighter .
public class Soldier {
public String Name;
public int Health;
protected int maxHealthPoints = 100;
public Soldier( String name){
Name = name;
}
public void treat(){
Health = maxHealthPoints;
System. out .println(Name);
}
}
public class Hero extends Soldier {
protected int maxHealthPoints = 500;
public Hero( String name) {
super(name);
}
}
* This source code was highlighted with Source Code Highlighter .
public class SoldiersIterator {
private Army _army;
boolean heroIsIterated;
int currentGroup;
int currentGroupSoldier;
public SoldiersIterator(Army army) {
_army = army;
heroIsIterated = false ;
currentGroup = 0;
currentGroupSoldier = 0;
}
public boolean hasNext() {
if (!heroIsIterated) return true ;
if (currentGroup < _army.ArmyGroups.size()) return true ;
if (currentGroup == _army.ArmyGroups.size()-1)
if (currentGroupSoldier < _army.ArmyGroups. get (currentGroup).Soldiers.size()) return true ;
return false ;
}
public Soldier next() {
Soldier nextSoldier;
// we still not iterated all soldiers in current group
if (currentGroup < _army.ArmyGroups.size()) {
if (currentGroupSoldier < _army.ArmyGroups. get (currentGroup).Soldiers.size()) {
nextSoldier = _army.ArmyGroups. get (currentGroup).Soldiers. get (currentGroupSoldier);
currentGroupSoldier++;
}
// moving to next group
else {
currentGroup++;
currentGroupSoldier = 0;
return next();
}
}
// hero is the last who left the battlefield
else if (!heroIsIterated) {
heroIsIterated = true ;
return _army.ArmyHero;
} else {
// THROW EXCEPTION HERE
throw new IllegalStateException( "End of colletion" );
//or set all counters to 0 and start again, but not recommended
}
return nextSoldier;
}
}
* This source code was highlighted with Source Code Highlighter .
SoldiersIterator iterator = new SoldiersIterator (earthArmy);
* This source code was highlighted with Source Code Highlighter .
IIterator iterator = AbstractArmy.GetSoldiersIterator ();
* This source code was highlighted with Source Code Highlighter .
var list = new List < int > ();
//GetEnumerator is method of IEnumerator (Aggregate)
var enumerator = list.GetEnumerator ();
//MoveNext method of IEnumerable (Iterator)
enumerator.MoveNext ();
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/84184/
All Articles