Create intervals of size N from a given integer

Lets say I have an integer for example 6350 . How can I create intervals of size N (lets say 500) that results in the following ranges:

[0, 500, 1000, 1500, 2000 ... 6000, 6350]

Answer

The most obvious java only (no third parties) solution:

int max = 6350;
int N = 500;

List<Integer> result = new ArrayList<>();

for(int i = 0 ; i < max; i+=N) {
    result.add(i);
}

result.add(max);

The result array list should look like exactly as you’ve posted in the question

Leave a Reply

Your email address will not be published. Required fields are marked *