For two variable calls, @ x4u is correct. The third call to the variable will be much more difficult to imitate.
For example, I think linspace (1,30,60) should give values 1, 1.5, 2, 2.5, 3, 3.5 ... or maybe linspace values (1,30,59) - - in any case , same problem.
. for.
counter=new Linspace(1,30,60);
while(counter.hasNext()) {
process(counter.getNextFloat())
}
while(float f : new Linspace(1,30,60)) {
process(f);
}
Linspace, Iterable.
- , , , , .
:
(. , , ! , , , end < start , , .)
public class Linspace {
private float current;
private final float end;
private final float step;
public Linspace(float start, float end, float totalCount) {
this.current=start;
this.end=end;
this.step=(end - start) / totalCount;
}
public boolean hasNext() {
return current < (end + step/2);
}
public float getNextFloat() {
current+=step;
return current;
}
}