Arrayindex out of bounds
error is showing up and I have particularly no idea why it is hapening. I am trying create 2N tuple objects and trying to put them in a array of size 2N
Tuple[] minuteunit = new Tuple[2*N];
if(!intervals.isEmpty())
{
for(i = 0; i < ((2*N)-1); i = 1+2)
{
minuteunit[i] = new Tuple(intervals.get(i).getBeginMinuteUnit(),"s");
minuteunit[i+1] = new Tuple(intervals.get(i).getEndMinuteUnit(),"e");
}
it is most likely the face you are using i
in intervals.get(i)
, because i
is incrementing +2
. I would imagine you have N
values in intervals
and therefore when i >= (N/2)
you get an overflow.
Try this:
for(i = 0; i < N; i++)
{
minuteunit[2*i] = new Tuple(intervals.get(i).getBeginMinuteUnit(),"s");
minuteunit[2*i+1] = new Tuple(intervals.get(i).getEndMinuteUnit(),"e");
}
Also, assuming intervals should contain N
entries, you could update your intervals.isEmpty()
check to:
if(intevals.size() == N)
{
...
for(i = 0; i < ((2*N)-1); i = 1+2)
At each iteration i equals 3, so it never finishes. I think you want to add 2 at each iteration so use:
for(i = 0; i < ((2*N)-1); i = i+2)
You need to make your loop's termination condition i<2*N-2
, otherwise the iteration of adding 2 will push i
over the end of your array when i
is 2*N-2
(which it wi eventually be).
btw, I assume that your iteration of i=1+2
is a typo, and you have actually coded i=i+2
I'm going to take a shot in the dark here and guess that maybe intervals.get(i)
is what's actually causing your problem here and not the array itself. Ignoring what appears to be a typo in the increment, the condition for your loop seems to be fine, since if i < 2*N - 1
, then i + 1 < 2*N
and so there's no out of bound index for the array . That leaves the intervals.get(i)
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.