-1

I have to implement class Incrementer and it suppose to implement Iterable I do not really get why it should implement Iterable<Integer> instead of Iterator<Integer>? I do not ask for solution just the usage of Interfaces.

Why the method in(int,int) is static?

public class Test {

  public static void main(String[] args) {

    for(int k : in(1, 10)) System.out.print(k + " ");
    System.out.println();

    for(int k : in(1, 10).by(2)) System.out.print(k + " ");
    System.out.println();

    for(int k : in(10, 1)) System.out.print(k + " ");
    System.out.println();


    for(int k : in(1, 10).by(-1)) System.out.print(k + " ");
    System.out.println();

    Incrementer inc;
    for (int i : inc = in(1,10) ) {
      if (i == 4) inc.by(2);
      System.out.print(i + " ");
    }
    System.out.println();
    for (int i : inc = in(1,10) ) {
      if (i == 8) inc.by(-2);
      System.out.print(i + " ");
    }
    System.out.println();
    for(int k : inc = in(10, 1)) {
      if (k == 5) inc.by(1);
      System.out.print(k + " ");
    }

  }  
}

Output:

1 2 3 4 5 6 7 8 9 10 
1 3 5 7 9 
10 9 8 7 6 5 4 3 2 1 
10 9 8 7 6 5 4 3 2 1 
1 2 3 4 6 8 10 
1 2 3 4 5 6 7 8 6 4 2 
10 9 8 7 6 5 6 7 8 9 10
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • If you don't implement `Iterable`, you cannot use your class in a foreach loop as you currently do. – fge Mar 22 '14 at 12:26

2 Answers2

3

Iterable class has a method iterator() which returns an instance of Iterator class which can be used to iterate objects.Once you have Iterator class instance,you can use hasNext() and next() methods to iterate the elements in your class

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • Thank you but where should I keep field `val` representing Integer in class which implements `Iterator` or `Iterable`? – Yoda Mar 22 '14 at 12:30
  • 3
    The most visible change of this interface however is that it allows usage in foreach loops – fge Mar 22 '14 at 12:35
  • Why the method `in(int,int)` is static? – Yoda Mar 22 '14 at 12:35
  • @fge,yes you are 100% right.Forgot to mention that in my answer.We can just as well have a method which gives an instance of Iterator but would not be able to use in a for each loop. – Kumar Abhinav Mar 22 '14 at 12:40
  • 1
    +1 to your comment,post this as a answer – Kumar Abhinav Mar 22 '14 at 12:40
  • Why? Just edit your answer and add it ;) – fge Mar 22 '14 at 12:46
  • 1
    @fge Its a strong point ,the only reason why we should implement an Iterable instead of working on an Iterator instance directly.therefore you should post this point as a separate answer,including the concept of for each loop – Kumar Abhinav Mar 22 '14 at 12:52
  • Thank you really much!!!!!!!!!!! Please could take a look at solution please: http://stackoverflow.com/questions/22577870/iterator-returns-wrong-integer-values – Yoda Mar 22 '14 at 12:54
1

While designing a class if we want to provide iterator functionality , we implement Iterable interface. Now Iterable interface has method Iterator<T> iterator() so u need to write implementation of iterator inside your class.

You should look at source code of collection classes ( which implements Collection Interface ) they have same kind of implementation.

So basically you need to use both interfaces to provide iterator functionality in your class.

Vipin
  • 4,851
  • 3
  • 35
  • 65