Re: array problem
Re: array problem
- Subject: Re: array problem
- From: Ben Dougall <email@hidden>
- Date: Thu, 11 Mar 2004 15:20:38 +0000
On Thursday, March 11, 2004, at 12:47 pm, Sascha Kuehn wrote:
hi,
i have a problem with creating an array of arrays
this is my code:
-----------------------------------------------------------------------
-
-
- (void)makeMatrix
{
int i, j;
matrixArray = [[NSMutableArray array] retain];
temp = [[NSMutableArray array] retain];
for(i=0; i<x; i++) // x is an int between 3 and 5
{
for(j=0; j<x; j++) // x is an int between 3 and 5
{
[temp addObject:[NSNumber numberWithInt:0]];
}
[matrixArray addObject:temp];
}
[temp release];
}
-----------------------------------------------------------------------
-
-
what i need is an array of arrays with numbers.
in case of x = 3 like this: {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
in case of x = 4 like this: {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
{0, 0, 0, 0}}
in case of x = 5 like this: {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0,
0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}}
and so on.
but my method don't do that :(
see if this works:
- (void)makeMatrix
{
int i, j;
matrixArray = [[NSMutableArray alloc] initWithCapacity:x];
for(i=0; i<x; i++) // x is an int between 3 and 5 {
temp = [[NSMutableArray alloc] initWithCapacity:x];
for(j=0; j<x; j++) // x is an int between 3 and 5 {
[temp addObject:[NSNumber numberWithInt:0]];
}
[matrixArray addObject:temp];
[temp release];
}
}
the main difference is the 2nd level (as in not the root) arrays are
created per i loop rather than just once -- obviously you'll need (and
therefore need to create) x number of arrays to go in the matrixArray
not just one. also changed to alloc & init creation just because you
can so you might aswell. and also the array release is in the i loop to
balance each, rather than just one, instance of 2nd level arrays.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.