我们可以动态创建n个NSMutableArray吗?

时间:2015-06-12 12:58:10

标签: ios objective-c arrays nsmutablearray

我从用户那里获得了n值,根据它我要创建n个NSMutableArray

3 个答案:

答案 0 :(得分:3)

是的,你可以,只需将数组添加到另一个NSMutableArray。使用此代码:

NSMutableArray *outerArray = [NSMutableArray arrayWithCapacity:n];

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

答案 1 :(得分:1)

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 ++;
} 

答案 2 :(得分:0)

使用数组数组:

NSMutableArray *arrays = [NSMutableArray new];

while (gotMoreInput) {
    NSMutableArray *array = [NSMutableArray new];
    [array addObject:@"Some data"];
    [arrays addObject:array];
}