BruceEckel / OnJava8-Examples

Code Examples for the book "On Java 8"

Home Page:http://www.OnJava8.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Please check onjava/Range.java

ittimeline opened this issue · comments

  // Produce sequence [start..end) incrementing by step
  public static
  int[] range(int start, int end, int step) {
    int sz = (end - start)/step;
    int[] result = new int[sz];
    for(int i = 0; i < sz; i++)
      result[i] = start + (i * step);
    return result;
  }

I think this range method run result is wrong..
such as if start is 10,end is 21, step is 3 .
The result is [10, 13, 16]
The right result is [10,13,16,19]

The correct implementation would look like this?

public static int[] range(int start, int end, int step) {
  int sz = (end - start)/step+1;
  int[] result = new int[sz];
  for(int i = 0; i < sz; i++)
    result[i] = start + (i * step);
  return result;
}

Thanks! Latest version fixes this.