Re: Simple way to traverse 2D array?
Re: Simple way to traverse 2D array?
- Subject: Re: Simple way to traverse 2D array?
- From: Graham Cox <email@hidden>
- Date: Tue, 24 Feb 2009 12:32:02 +1100
On 24/02/2009, at 11:36 AM, Ashley Perrien wrote:
I'm hoping there's a simple way to do this or perhaps I can get some
ideas on which direction to go. I've built a 2D NSArray of NSArrays.
The primary array will be of variable length (usually less than 10)
and the secondary arrays will also be variable, usually less than 5,
but each one may be different, the first may be 2, the second may be
3, etc. I need to traverse them and build all the unique
combinations of the elements.
For instance, I have
NSArray *numbers containing 1,2 and 3
NSArray *letters containing A and B
NSArray *colors containing Red, White, Blue.
I need to build an array of objects we'll call combos:
combo1: 1, A, Red
combo2: 1, A, White
combo3: 1, A, Blue
combo4: 1, B, Red
combo5: 1, B, White
.
.
.
combo18: 3, B, Blue
This is relatively easy if I know how many arrays I'm working with
(3 in this case) to simply nest the for loops but if I don't know
how many arrays the primary array has, I can't think of a way to
nest the loops if I don't know how deeply to nest them. Any
suggestions on the best way to approach something like this?
It's a bit unclear from your description if what you have is really a
3D array or a 2D array. Each of the combinations in your example
output has three dimensions, not two.
It sounds like what you really want is to enumerate all of the
combinations of three separate arrays. If that's the case, you might
be muddling yourself by creating arrays of arrays. Instead, just keep
each array as a simple 1D array and combine them. Nested for loops of
each from 0..[array count] will do it. If the "primary array" is
merely a list of numbers from 0..count, it doesn't even need to exist.
The number of combinations is simply count(a) x count(b) x...x
count(n) where n is the number of dimensions.
e.g.
- (void) combinations
{
NSArray* numbers = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSArray* letters = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D",
nil];
NSArray* colours = [NSArray arrayWithObjects:@"Red", @"White",
@"Blue", nil];
unsigned i, j, k;
NSString* combo;
for( i = 0; i < [numbers count]; ++i )
{
for( j = 0; j < [letters count]; ++j )
{
for( k = 0; k < [colours count]; ++k)
{
int index = (i * [letters count] * [colours count]) + (j *
[colours count]) + k;
combo = [NSString stringWithFormat:@"combo%d: %@, %@, %@", index +
1, [numbers objectAtIndex:i], [letters objectAtIndex:j], [colours
objectAtIndex:k]];
NSLog(@"%@", combo);
}
}
}
}
This might also help, depending on what you're really trying to do: http://en.wikipedia.org/wiki/Combination
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden