PDA

View Full Version : NSMutableArrays




apffal
Mar 3, 2008, 06:25 AM
In my application, I want to compare floats in two NSMutableArrays - one sorted and the other unsorted - and get some action accordingly that comparison.
After each step, I want to remove the first group of floats in the unsorted array and go to next step.
However, always get a runtime error "index (0) beyond bounds (0)".
What is wrong ?
Thanks for help.

That is my code.


- (IBAction)calc:(id)sender {

calc = [[Calc alloc]init];
[calc setNum:[numField intValue]];
num = [calc get_num_value];
int vot_count = [votos count];

for(i = 0; i < vot_count; ++i)
for(j = 1; j < num + 1; ++j)
{
vot = [[votos objectAtIndex:i] intValue];
v = vot / j;
[ordem addObject:[NSNumber numberWithFloat:v]];
[quoc addObject:[NSNumber numberWithFloat:v]];
}

[ordem sortUsingSelector:@selector(compare:)];
for (i = 0; i< (floor([ordem count]/2.0)); i++)
[ordem exchangeObjectAtIndex:i withObjectAtIndex:([ordem count]-(i + 1))];

for(i = 0; i < vot_count; i++)
{
if([[quoc objectAtIndex:i] floatValue] < [[ordem objectAtIndex:num -1] floatValue])
man = 0;

for(j = 0; j < num; j++)
{
if([[quoc objectAtIndex:j] floatValue] >= [[ordem objectAtIndex:num -1] floatValue]) {
man = j + 1;
}
else
break;
}

[mand addObject:[NSNumber numberWithInt:man]];
[listBox_3 setDataSource:self];
[listBox_3 reloadData];

for(k = 0; k < num; k++)
[quoc removeObjectAtIndex:0];
}
}



iSee
Mar 3, 2008, 09:12 AM
Your program is trying to to use an invalid index in an array. Specifically, it looks like it is trying to access an object at index 0 on an array that is empty.

Run your program using the debugger to see exactly what line is causing the problem and trace back the issue from there.

apffal
Mar 3, 2008, 10:14 AM
The debugger says that the line is causing the problem is this

[quoc removeObjectAtIndex:0];

apffal
Mar 3, 2008, 10:14 AM
The debugger says that the line is causing the problem is this

[quoc removeObjectAtIndex:0];

iSee
Mar 3, 2008, 10:51 AM
So you're trying to remove more objects than are in the array.

One thing you might try is making all the variables that you are using like local variable (i, j, k, num, ordem, quoc, etc.) actual local variables (that is, declare them within the scope of the calc method.

The value of these variables could be getting changed by other methods. That could account for this problem. There's no way to tell from looking at this fragment, but it's generally a good idea anyway.

By the way, you can put your code between ... tags to preserve indenting.