3

Possible Duplicate:
How do I create a multidimensional array?

I'm new to programming and objective C, so although I have found a few questions on here that discuss multidimensional arrays, I'm not quite getting what I need to do in order to build and use my own.

I need to make an array that has 16 rows and 3 columns. The array needs to accept string objects. I do not know how to create this, fill it, or access its contents. Would anyone be kind enough to break it down for me?

Community
  • 1
  • 1
jac300
  • 5,182
  • 14
  • 54
  • 89
  • Fixed size at 16x3 or dynamically expanding in one direction or two directions? Call it professional curiosity, why exactly do you need a multidimensional array anyhow? – slf Oct 23 '12 at 15:58
  • I'm making a tic tac toe game, and I'm tracking all the possible moves of each player. – jac300 Oct 23 '12 at 15:59
  • and its a fixed size only because I'm not sure how I would yet know how to deal with one that is dynamically expanding – jac300 Oct 23 '12 at 16:00
  • With a fixed size, why not just have a single, immutable array? I see no need for a multi-dimensional one, or am I missing something? – slf Oct 23 '12 at 16:01
  • To Zoul: my apologies for duplication, I looked at some of the duplicates you mentioned and these would not have made sense to me. Please understand that as a beginner, it is difficult to apply other's specific issues to your own, even if it is of the same topic. – jac300 Oct 23 '12 at 16:57
  • To slf: thank you for that point. I thought it would be easier to keep mental track of what was going where with separate arrays for each column, row, etc... but perhaps it is worth trying to do it in one array. – jac300 Oct 23 '12 at 16:58

1 Answers1

10

I'd suggest you use a C-array, as an NSArray doesn't support multiple dimensions. You could declare the array you described like this:

NSString *stringArray[16][3];

Setting and accessing any string of this array is quite straight-forward:

stringArray[7][1] = @"Stringstringstring";

NSString *string = stringArray[3][0];

However, you could use an NSArray (or NSMutableArray), but that would be a bit less elegant:

NSArray *stringArray = [NSArray arrayWithObjects:
                        [NSMutableArray array],
                        [NSMutableArray array],
                        [NSMutableArray array], nil];

Those three NSMutableArrays would be the three columns of your two-dimensional array.

Edit

Using an NSArray, it might be easier to use a loop to fill it:

NSMutableArray *stringArray = [NSMutableArray array];

for (int column = 0; column < 3; column++)
{
    NSMutableArray *columnArray = [NSMutableArray array];

    for (int row = 0; row < 16; row++)
        [columnArray addObject:[NSString stringWithFormat:@"Row %i, column %i", row, column]];

    [stringArray addObject:columnArray];
}
Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
  • That works great, thank you. By the way, is there a way to add an object to more than one cell at a time? As in 'stringArray[7][1],[3][4] = @"stringObject";' or do I have to write a separate statement for each? – jac300 Oct 23 '12 at 16:39
  • You'd have to write a separate statement for each. – Tim Vermeulen Oct 23 '12 at 16:40