Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
public class AirForceIterator implements Iterator { List
jets; IAircraft[] helis; LinkedList
cargo; int jetsPosition = 0; int helisPosition = 0; int cargoPosition = 0; /** * The iterator is composed with the object it'll be iterating over */ public AirForceIterator(AirForce airForce) { jets = airForce.getJets(); helis = airForce.getHelis(); cargo = airForce.getCargo(); } @Override /** * We provide our own custom logic of returning aircraft in a * sequence. Note we are returning IAircraft interface object which * every plane in our airforce implements. We also design the function * to throw a runtime exception if next is invoked when no more elements * are left to iterate over */ public IAircraft next() { // return helis first if (helisPosition < helis.length) { return helis[helisPosition++]; } // return jets second if (jetsPosition < jets.size()) { return jets.get(jetsPosition++); } // return cargos last if (cargoPosition < cargo.size()) { return cargo.get(cargoPosition++); } throw new RuntimeException("No more elements"); } @Override public boolean hasNext() { return helis.length > helisPosition || jets.size() > jetsPosition || cargo.size() > cargoPosition; } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)