Is there a java version of matlab’s colon operator or linspace? For instance, I’d like to make a for loop for evenly spaced numbers, but I don’t want to bother with creating an array of those numbers manually.
For example to get all integers from 1 to 30, in matlab I would type:
1:30
or
linspace(1,30)
For the two variable call, @x4u is correct. The three variable call will be quite a bit harder to emulate.
For instance, i think that linspace(1,30,60) should produce values 1, 1.5, 2, 2.5, 3, 3.5…, or maybe that’s the values for linspace(1,30,59)–either way, same problem.
With this format you’ll have to do the calculations yourself–Personally I’d create a new object to do the whole thing for me and forget the for loop.
or simply
if you have your Linspace object implement Iterable.
Then the inside of the counter object should be pretty obvious to implement and it will easily communicate to you what it is doing without obfuscating your code with a bunch of numeric calculations to figure out ratios.
An implementation might be something like this:
(NOTE: Untested and I’m pretty sure this would be vulnerable to edge cases and floating point errors! It also probably won’t handle end < start for backwards counting, it’s just a suggestion to get you going.)