Last active
December 19, 2016 15:40
-
-
Save bbrother92/d4a4babaaff123cc8a590dfa5eb3bb53 to your computer and use it in GitHub Desktop.
#collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String... args) { | |
Box bx = new Box(); | |
for (Human o : bx) { | |
System.out.println(o); | |
} | |
} | |
} | |
class Box implements Iterable<Human> { | |
Human[] items = {new Human("John"), new Human("Bernard"), new Human("Marlon")}; | |
@Override | |
public Iterator iterator() { | |
return new Iterator() { | |
int index = 0; | |
@Override | |
public boolean hasNext() { | |
return index < items.length; | |
} | |
@Override | |
public Object next() { | |
return items[index++]; | |
} | |
}; | |
} | |
} | |
class Human { | |
String name; | |
public Human(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return name.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment