There is nothing built-in for this, but it is relatively simple to implement a range such as an immutable Iterable<Long> (or Integer or something else). Just create a custom Iterator that starts with an initial value, and then increment for each call to next() until you pass the final value. You have to decide how and if you want to handle high iterative iteration and so on, but itβs not difficult. You can also do this as an immutable List implementation, where the value for each index is calculated on demand ( start + index * increment ).
While your question relates to creating a range-based βarrayβ, an array full of data in the entire range is often not needed, especially if you just want to iterate over the numbers in the range. If that's all you need, you end up repeating the numbers in the range twice to create an array or List , and then read it. Using the lazy range iterator, as I described, does not have this drawback. In addition, a lazy iterator can be easily copied to a List if you want all values ββto be stored directly in memory. The only drawback to this compared to building an array is some of the official duties.
source share