Can we create n no.of NSMutableArray dynamically?

92 views Asked by At

I'm getting the n value from the user, according to it I want to create n number of NSMutableArray .

3

There are 3 answers

0
Droppy On

By using an array of arrays:

NSMutableArray *arrays = [NSMutableArray new];

while (gotMoreInput) {
    NSMutableArray *array = [NSMutableArray new];
    [array addObject:@"Some data"];
    [arrays addObject:array];
}
0
humblePilgrim On
int n= 5; // or whatever user gives you
count = 0;
NSMutableArray *arrayOfArrays = [NSMutableArray array];

while(count < n)
{
   NSMutableArray *anArray = [NSMutableArray array];
   [arrayOfArrays addObject: anArray];
   count ++;
} 
1
Vinay Jain On

Yes you can, just add the arrays to another NSMutableArray. Use this code:

NSMutableArray *outerArray = [NSMutableArray arrayWithCapacity:n];

for (int i=0; i<n; i++) {
    NSMutableArray *innerArray = [[NSMutableArray alloc] init];
    [outerArray addObject:innerArray];
}