How to have an array which is indexed from minValueRange please?
You can't, in Java. Your best bet is just to do the math as necessary (but I mention a couple of alternatives below, which really just encapsulate it), e.g.:
int minValForRange = myDataObj.getMinValue();
int maxValForRange = myDataObj.getMaxValue();
String[] arrOfRangesForSelection = new String[maxValForRange - minValForRange + 1];
// Enough room for the range (inclusive) -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Logger.d(String.valueOf(minValForRange));
Logger.d(String.valueOf(maxValForRange));
for (int i = minValForRange; i <= maxValForRange; i++) {
arrOfRangesForSelection[i - minValForRange] = String.valueOf(i);
// The math ------------^^^^^^^^^^^^^^^^^^
}
(Note that in the above I'm assuming maxValForRange
should be included, because that's how you wrote your loop, which is why we have + 1
in the line allocating the array.)
If you didn't want maxValForRange
to be included (in programming, we usually include the lower bound but not the upper bound), you wouldn't have the + 1
on the array allocation and would use <
rather than <=
in the loop:
int minValForRange = myDataObj.getMinValue();
int maxValForRange = myDataObj.getMaxValue();
String[] arrOfRangesForSelection = new String[maxValForRange - minValForRange];
// No + 1 here --------------------------------------------------------------^
Logger.d(String.valueOf(minValForRange));
Logger.d(String.valueOf(maxValForRange));
for (int i = minValForRange; i < maxValForRange; i++) {
// Just < here ----------------^
arrOfRangesForSelection[i - minValForRange] = String.valueOf(i);
}
If you want, you can trivially write a class that will provide get
and set
methods which do the necessary math so you don't have to repeat it all over the code, and wrap your array in that class.
You can also do an ArrayList
subclass that does the necessary math.