fork download
  1.  
  2. import java.util.Iterator;
  3.  
  4. class Zakres implements Iterable<Integer> {
  5.  
  6. final int start;
  7. final int koniec;
  8.  
  9. public Zakres(final int start, final int koniec) {
  10. this.start = start;
  11. this.koniec = koniec;
  12. }
  13.  
  14. @Override
  15. public Iterator<Integer> iterator() {
  16. return new Iterator<Integer>() {
  17.  
  18. int aktualny = start;
  19.  
  20. @Override
  21. public boolean hasNext() {
  22. return aktualny <= koniec;
  23. }
  24.  
  25. @Override
  26. public Integer next() {
  27. return aktualny++;
  28. }
  29.  
  30. @Override
  31. public void remove() {
  32. throw new UnsupportedOperationException("Illegal action.");
  33. }
  34. };
  35. }
  36.  
  37. }
  38.  
  39. public class Main {
  40.  
  41. public static void main(String[] args) {
  42. Zakres zakres = new Zakres(5, 8);
  43. for (int i : zakres) {
  44. System.out.println(i);
  45. }
  46. for (int i : zakres) {
  47. System.out.println(i);
  48. }
  49. for (int i : zakres) {
  50. System.out.println(i);
  51. }
  52. }
  53. }
  54.  
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
5
6
7
8
5
6
7
8
5
6
7
8