0

i have the following code;

int minValForRange = myDataObj.getMinValue();
            int maxValForRange = myDataObj.getMaxValue();

            String[] arrOfRangesForSelection = new String[maxValForRange];

            Logger.d(String.valueOf(minValForRange));
            Logger.d(String.valueOf(maxValForRange));

            for (int i = minValForRange; i <= maxValForRange; i++) {
                arrOfRangesForSelection[i] = String.valueOf(i);
            }

minValForRange & maxValForRange are dynamic variables (in this case are 1 and 300) so it this case code above is throwing exception:

 length=300; index=300

How to have an array which is indexed from minValueRange please?

Many thanks for any advice.

redrom
  • 10,590
  • 29
  • 143
  • 244
  • you need to have array of size maxvalue + 1 so it will have max value as index – NightsWatch Oct 14 '15 at 12:33
  • Another take on your requirement could be to use some sort of Map style collection. There's an interesting discussion, and some suggested solutions, [here](http://stackoverflow.com/questions/12626135/memory-efficient-sparse-array-in-java) – fvu Oct 14 '15 at 12:40

3 Answers3

3

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.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • I don't understand you math. OP confused by seeing the output `length=300; index=300` in the stacktrace. – Suresh Atta Oct 14 '15 at 12:37
  • @sᴜʀᴇsʜᴀᴛᴛᴀ: The primary question, as I see it, is *"How to have an array which is indexed from minValueRange please?"* That said, I *also* addressed the issue of making sure the array is large enough for both bounds to be included. – T.J. Crowder Oct 14 '15 at 12:37
  • Better than doing the math "inline" like that, is to encapsulate the array in a class. – aioobe Oct 14 '15 at 12:40
  • @aioobe: Yes, I mentioned that from the outset (see the end). It depends, of course, on the scope of the array. If it were **only** the quoted code, a class would be overkill. But if there's more to it (and there probably is), I'd encapsulate. – T.J. Crowder Oct 14 '15 at 12:41
0

Access to array element is zero-based, so when you try to access maxValForRange, you have to have maxValForRange+1 size;

AnatolyS
  • 4,111
  • 14
  • 27
0

just use i < maxVal instead of i <= maxVal.

for (int i = minValForRange; i < maxValForRange; i++)
    arrOfRangesForSelection[i] = String.valueOf(i);