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.

2
  • you need to have array of size maxvalue + 1 so it will have max value as index Commented Oct 14, 2015 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 Commented Oct 14, 2015 at 12:40

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't understand you math. OP confused by seeing the output length=300; index=300 in the stacktrace.
@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.
Better than doing the math "inline" like that, is to encapsulate the array in a class.
@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.
0

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.