1

EveryBody..

i want to create one 8*8 dimensional array in objective c..

(
[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, 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, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
)

like that.. And also use as one of the object can moved in it.. Like want to move

MOVE_ARRAY = array([0, 0], [0, 2])

And also check the array's any position like 5*6 or 4*3 any.. So, Can any one help me about how to initialize , and how to use in code ? Thanks.

6
  • Do you know to create a multidimensional(2D) array in any other programming language? Also a 64D would redefine physics. I am assuming you need a 2D array. Commented Aug 29, 2011 at 7:42
  • 2
    Objective C is backward compatible with C/C++. You can create a simple C array and use it... if you cannot create that, you've a lot of ground to cover before you jump into iPhone development.. Commented Aug 29, 2011 at 7:50
  • @WTP i didn't told that array is 64D.. i just show you the position of the objects in. Commented Aug 29, 2011 at 8:33
  • @Praveen S.. yes i need 2D array and i displaying the positions of the objects.. Commented Aug 29, 2011 at 8:34
  • @lukya.. So i am not able to do iPhone development ? Commented Aug 29, 2011 at 8:35

3 Answers 3

7

In Objective-C:

array = [[NSMutableArray alloc] init];

for (int i = 0; i < 8; i++) {
    NSMutableArray *subArray = [[NSMutableArray alloc] init];
    for (int j = 0; j < 8; j++) {
        [subArray addObject:[NSNumber numberWithInt:0]]; 
    }
    [array addObject:subArray];
    [subArray release];
}

(array is an instance variable, that has to be added to your header file and released in you dealloc method)

To retrieve a value at a certain position you could write a method like this:

- (int)valueAtRow:(int)row andColumn:(int)col {
    NSMutableArray *subArray = [array objectAtIndex:row];
    return [[subArray objectAtIndex:col] intValue];
}

=== UPDATE ===

To remove an object you could do this:

- (void)removeObjectAtRow:(int)row andColumn:(int)col {
    NSMutableArray *subArray = [array objectAtIndex:row];
    [subArray removeObjectAtIndex:col];
}

You have to be careful though, because removing objects will change the structure of the array (e.g. the row where you removed an object will have only 7 items after the removal. So you might want to think about leaving the structure intact and set the values that you want to delete to a value that you normally don't use:

- (void)removeObjectAtRow:(int)row andColumn:(int)col {
    NSMutableArray *subArray = [array objectAtIndex:row];
    [subArray replaceObjectAtIndex:col withObject:[NSNumber numberWithInt:-999]];
}
Sign up to request clarification or add additional context in comments.

4 Comments

And If i want to remove the any position , So how i could ?
With such implementation it's not possible just to write array[i][j]. But question was in that. So -1...
Hey joerm... I m very thankful to you.. And need one more help. One more query, i m using this array and in running code i need to change index with another index.. How it ?
Hi Jagdish. I'm afraid I do not fully understand what you try to achieve. Do you want to swap two values? Like for example swap the value in row 1, column 2 with the value at row2 and column 1?
2

In C:

int **array = (int**)calloc(8, sizeof(int*));
for (int i=0; i<8; i++) array[i] = (int*)calloc(8, sizeof(int));
// use your array
// cleaning:
for (int i=0; i<8; i++) free(array[i]);
free(array);

Comments

2

To create a constant 2d array containing integers,simply do something like:

NSArray *array;
array = @[@[@1, @7, @4, @11],@[@2, @6, @9]];

This creates an array

1 7 4 11
2 6 9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.