Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a small isometric engine for my next iPhone game. To store the map cells or tiles I need a 2 dimensionel array. Right now I am faking it with a 1-dimensionel, and it is not good enough anymore.

So from what I have found out looking around the net is that in objective-c I need to make an array of arrays. So here is my question: How do I dynamicly create arrays at runtime based on how many map-rows I need?

The first array is easy enough:

NSMutableArray *OuterArray = [NSMutableArray arrayWithCapacity:mapSize];

now I have the first array that should contain an array for each row needed. Problem is, it can be 10 but it can also be 200 or even more. So I dont want to manually create each array and then add it. I am thinking there must be a way to create all these arrays at runtime based on input, such as the chosen mapsize.

Hope you can help me Thanks in advance Peter

share|improve this question

4 Answers 4

up vote 0 down vote accepted

The whole point of NSMutableArray is that you don't care. Initialize both dimensions with an approximate size and then add to them. If your array grows beyond your initial estimate the backing storage will be increased to accomodate it. This counts for both your columns (first order array), and rows (second order array).


EDIT

Not sure what you meant in your comment. But this is one way to dynamically create a 2-dimensional mutable array in Objective-C.

NSUInteger columns = 25 ; // or some random, runtime number
NSUInteger rows = 50;     // once again, some random, runtime number

NSMutableArray * outer = [NSMutableArray arrayWithCapacity: rows];

for(NSUInteger i; i < rows; i++) {
    NSMutableArray * inner = [NSMutableArray arrayWithCapacity: columns];
    [outer addObject: inner];
}

// Do something with outer array here
share|improve this answer
    
Hi, thanks for the answer, Though I also think you misunderstood the question, please see comment above –  Luffen Aug 8 '11 at 13:28
    
@Luffen - your question seems pretty straightforward. See my edits above and let me know if that is what your asking. –  Perception Aug 8 '11 at 13:39
    
That actually seems to do the trick, thank you very much! :) –  Luffen Aug 8 '11 at 13:48

I think this previous question should help.

2d arrays in objective c

Nothing to do with me. I have never owned an iphone or tried to write code for one.

share|improve this answer
    
thanks for the quick answer, I did not however find what I was looking for in the previous question –  Luffen Aug 8 '11 at 13:28
    
If that didn't help then I am out of my depth. In truth I probably was already, and for that reason I am out....Good Luck –  David Steele Aug 8 '11 at 13:44

NSMutableArray can hold as many elements as you want to add to it. (Based on available heap though).

All you have to do is when you want to add an element(Array) to this mutable array you can add it using the addObject method.

So you create a MutableArray as follows:

NSMutabaleArray *outerArray = [NSMutableArray array]; // initially contains 0 elements.

[outerArray addobject:<anotherArray>];
share|improve this answer
    
thanks for the answer, although I think you misunderstood, I know how to add something to the array, but my point is, I need to add maybe 100 arrays, and I need a way to create these arrays at runtime, so I dont have to manually type them. –  Luffen Aug 8 '11 at 13:27
    
I dont get it. What is meant by 100 arrays? The smaller array you would be adding to the bigger one? Basically a mutable element grows dynamically. –  Praveen S Aug 8 '11 at 13:33
    
You create them just like the top level array and fill them manually. Where or how you get your data for each item in the "sub"arrays is up to you. You can write a loop to add each subarray: for (i = 0; i < mapSize; i++) [outerArray addObject: [[NSMutableArray alloc] init]];. –  Rudy Velthuis Aug 8 '11 at 13:35
    
but what I need is to create the subarrays at runtime. Lets say I choose mapSize 100, then I need 100 arrays put in to the OuterArray. So what I need help with is creating these 100 arrays at runtime, because mapsize can be anything, I cant type them in beforehand –  Luffen Aug 8 '11 at 13:37
    
You can create NSMutableArrays and add them to the main NSMutableArray. –  Praveen S Aug 8 '11 at 13:39

From your rejection of the other answers I think you don't know how to add them in a loop, or am I wrong?

Try:

for (i = 0; i < mapSize; i++) 
    [outerArray addObject: [[NSMutableArray alloc] init]];

or, if you know or can estimate the size of the second dimension:

for (i = 0; i < mapSize; i++) 
    [outerArray addObject: [[NSMutableArray alloc] 
                            initWithCapacity: your_2nd_d_size]];

Now, how you fill the arrays, i.e. where you get the contents depends on you. In one or more loops, you do:

[(NSMutableArray *)[outerArray objectAtIndex: i] 
    addObject: your_current_object]; 
share|improve this answer
    
Hi Rudy, thanks for the answer, also got a working one from Perception :) –  Luffen Aug 9 '11 at 11:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.