Amazon.com Widgets multi dimensional array easy question
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 02, 2014, 04:42:10 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Help!!!
| | |-+ multi dimensional array easy question
Pages: [1] Go Down
Print
Author Topic: multi dimensional array easy question (Read 5049 times)
kapusadam
Newbie
*
Posts: 9


Email




on: March 23, 2010, 06:55:51 AM

Hello!

It's me again!
My question is, that I want to have a multi dimensional array.
Now I must allocate space for them. How do I do it?
Because I done it like this. GIves me a warning, then, when a I run it breaks at 2.
What can I do? THX 4 the answer

1.NSMutableArray **name;
2.name = [[NSMutableArray alloc] init];
3.   for(int i=0;i<10;++i){
4.      roi_pontok = [[NSMutableArray alloc] init];
5.     }
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: March 23, 2010, 10:32:48 AM

A mutl-dimensional array object is just an array whose elements themselves are arrays, like so:

Code: (Objective-C)
NSMutableArray *matrix = [NSMutableArray array];

for (int i  = 0; i < 10; ++i)
    [matrix  addObject: [NSMutableArray array]];

To get the first element of the first array in matrix, use this:

Code: (Objective-C)
NSMutableArray *row = [NSMutableArray objectAtIndex: 0];
object = [row objectAtIndex: 0];

Cheers,

Steve Kochan
Logged
kapusadam
Newbie
*
Posts: 9


Email




Reply #2 on: March 24, 2010, 12:49:07 AM

THX, again but when I run it it doesn't work. It won't write 3. So it got lost after 2. With this message:
NSMutableArray may not respond to +objectAtIndex

Here is my code:

NSMutableArray *matrix = [NSMutableArray array]; 
   NSLog( @"1");
   for (int i  = 0; i < 10; ++i) 
      [matrix  addObject: [NSMutableArray array]]; 
   NSLog( @"2");
   NSMutableArray *row = [NSMutableArray objectAtIndex: 0];
   NSLog( @"3");
   [row addObject: [NSNumber numberWithDouble: 4]];
   NSLog( @"4");
   id object = [row objectAtIndex: 0]; 
   NSLog( @"5");
   NSLog( @"Object %e ",object);
   NSLog( @"End");


How could I solve this?
THX again for help!

Adam
Logged
kapusadam
Newbie
*
Posts: 9


Email




Reply #3 on: March 24, 2010, 01:00:39 AM

Ok i found what the problem was.
NSLog( @"2");
   NSMutableArray *row = [matrix objectAtIndex: 0];
   NSLog( @"3");

THX! For giving me a hint where to start!!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #4 on: March 24, 2010, 02:50:31 AM

Oops!  Excuse the typing.  Meant to type:

Code: (Objective-C)
NSMutableArray *row = [matrix objectAtIndex: 0];  
object = [row objectAtIndex: 0]; 

Cheers,

Steve
Logged
kapusadam
Newbie
*
Posts: 9


Email




Reply #5 on: March 25, 2010, 04:16:31 AM

Thank u very much!

And I have another question:

How can I store a struct type variables with a 2D array? And how can I add it?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #6 on: March 25, 2010, 04:22:51 AM

You can't store a structure into an NSArray, as it only takes objects.   You can make a class whose instance variables correspond to the structure members and store objects from that class into the array.  That's assuming I understood your question correctly.

Cheers,

Steve
Logged
dcc1165
Newbie
*
Posts: 14


Email




Reply #7 on: April 13, 2010, 02:12:49 PM

I had the same question about multi-dimensional arrays and, being a good Forum Citizen, I searched before posting and found this thread Smiley.  It has helped quite a bit.

First, a criticism of ObjC (feel free to rebut) -- Multidimensional arrays seem VERY wordy.  From what I can tell (based on the above sample), there is no "short hand" way of accessing individual elements.  For example, in C:
Code: (Objective-C)
// To get row 5, col 1 of matrix
element5-1=matrix[5][1]

But in ObjC, it seems a little more wordy:
Code: (Objective-C)
row5=[matrix objectAtIndex:5];
col1=[row5 objectAtIndex:1];

In and of itself, this seems pretty straight forward, but when dealing with many different operations on different parts of the table (array of arrays), inside a loop structure, it can be quite arduous.

On a related, but different note, I created the following test program (can be pasted in its entirety to a new Cocoa Command Line Tool project):
Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

     NSMutableArray *table;// = [[NSMutableArray alloc] init];
     NSMutableArray *row; //= [[NSMutableArray alloc] init];

     int i,j;

// Load the table
     for (i=0;i<10;i++)
     {
           NSLog(@"Loading row %d", i);
           [table addObject:[NSMutableArray array]];
           row = [table objectAtIndex:i];
           for (j=0;j<10;j++)
               [row addObject:[[NSMutableString alloc] initWithFormat: @"%d", j]];
     }

     NSLog(@"================================\n\n");
     for (i=0;i<10;i++)
     {
         NSArray *tmprow=[table objectAtIndex:i];
         for (j=0;j<10;j++) {
              NSString *obj=[tmprow objectAtIndex:j];
              NSLog(@"Row: %d, Col:%d:, %@",i,j,obj);
         }
     }

    [pool drain];
    return 0;
}

The array of arrays (elements of each row) are always NULL.  Can someone explain why this is or what I did wrong?

Thanks Smiley
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #8 on: April 13, 2010, 02:23:37 PM

You need to allocate table.  Remove the comment from this line and it will work:

Code: (Objective-C)
    NSMutableArray *table;// = [[NSMutableArray alloc] init];

(Remember to release table when you're done with it.)

Yes, they're awkward to work with.  How about a macro?

Code: (Objective-C)
#define table(row,col)    [[table objectAtIndex: row] objectAtIndex: col]

This isn't very efficient for sequencing through the matrix (the row is extracted for each column), but you get the idea.

You can also use fast enumeration to iterate:

Code: (Objective-C)
for (NSMutableArray *row in table)
    for (NSString *colObj in row)
          ...

Cheers,

Steve Kochan
Logged
dcc1165
Newbie
*
Posts: 14


Email




Reply #9 on: April 13, 2010, 02:49:04 PM

You need to allocate table.  Remove the comment from this line and it will work:

Code: (Objective-C)
    NSMutableArray *table;// = [[NSMutableArray alloc] init];

ugh...I hate when I do things like that.  I don't even remember why I had it commented out...

I'm curious, though...the way I had it was defining the identifier, type, etc., but why didn't the "addObject:" method throw an error?  I would think if the memory wasn't allocated, the addObject should've caused a run-timr error.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #10 on: April 13, 2010, 07:49:54 PM

You may have gotten lucky and had table set to 0 (remember local variables have no default initial value).  In that case the addObject: message would be sent to a nil object.  You can do that and the effect is that nothing happens.

Cheers,

Steve 
Logged
dcc1165
Newbie
*
Posts: 14


Email




Reply #11 on: April 13, 2010, 11:51:12 PM

You can also use fast enumeration to iterate:

Code: (Objective-C)
for (NSMutableArray *row in table)
    for (NSString *colObj in row)
          ...

Hmm...that makes sense, but in my above loops, I'm using [table objectAtIndex:i] and [row objectAtIndex:j].  How would I access each of the individual objects without using the "i" and "j" loop vars?
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.