Iphone 如何创建此数组?
我需要创建一个名为BigArray的数组。BigArray需要包含以下数组中的单词 有三个数组称为“用户”。每个这样的数组中都有特定的单词,如: 用户1:猫、狗、蝙蝠 用户2:苹果、芒果、香蕉、梨 用户3:大,小 我想按以下方式将上述单词排列在BigArray中: 猫,苹果,大,狗,芒果,小,蝙蝠,香蕉,梨 我该怎么做Iphone 如何创建此数组?,iphone,objective-c,cocoa-touch,ios4,sorting,Iphone,Objective C,Cocoa Touch,Ios4,Sorting,我需要创建一个名为BigArray的数组。BigArray需要包含以下数组中的单词 有三个数组称为“用户”。每个这样的数组中都有特定的单词,如: 用户1:猫、狗、蝙蝠 用户2:苹果、芒果、香蕉、梨 用户3:大,小 我想按以下方式将上述单词排列在BigArray中: 猫,苹果,大,狗,芒果,小,蝙蝠,香蕉,梨 我该怎么做 创建一个NSMutable数组FinalArray 运行每个较小的阵列 将索引为0到n的每个较小数组中的每个对象添加到同一forloop中的最终数组中 即使您的Userx阵列并非
即使您的Userx阵列并非都具有相同的大小,此解决方案也会起作用:
NSMutableArray BigArray = [[NSMutableArray alloc] init];
NSMutableArray u1 = [Users1 mutableCopy];
NSMutableArray u2 = [Users2 mutableCopy];
NSMutableArray u3 = [Users3 mutableCopy];
while ([u1 count] || [u2 count] || [u3 count]) {
if ([u1 count]) {
[BigArray addObject:[u1 objectAtIndex:0]];
[u1 removeObjectAtIndex:0];
}
if ([u2 count]) {
[BigArray addObject:[u2 objectAtIndex:0]];
[u2 removeObjectAtIndex:0];
}
if ([u3 count]) {
[BigArray addObject:[u3 objectAtIndex:0]];
[u3 removeObjectAtIndex:0];
}
}
[u1 release];
[u2 release];
[u3 release];
/* BigArray now contains what you need */
首先想到的是一个稍微复杂的循环(也许有一种更优雅的方式,不知道)
booldone=FALSE;
NSArray*数组=[NSArray数组及其对象:users1、users2、users3、nil];
NSArray*阵列;
NSMutableArray*bigArray=[NSMutableArray阵列容量:users1.count+users2.count+users3.count];
int i=0;
int j;
而(!完成){
完成=正确;
对于(j=0;j
看看这是否有帮助
NSArray *a1 = [[NSArray alloc] initWithObjects:@"Cat", @"Dog", @"Bat",nil];
NSArray *a2 = [[NSArray alloc] initWithObjects:@"Apple", @"Mango", @"Banana", @"Pear",nil];
NSArray *a3 = [[NSArray alloc] initWithObjects:@"Big", @"Small",nil];
NSUInteger arrayLength = [a1 count]+[a2 count]+[a3 count];
NSMutableArray *finalMutableArray = [NSMutableArray arrayWithCapacity:(arrayLength)];
for (NSUInteger index = 0; index < [a2 count]; index++) { // take the size of the biggest array
if(index < [a1 count])
[finalMutableArray addObject:[a1 objectAtIndex:index]];
[finalMutableArray addObject:[a2 objectAtIndex:index]];
if(index < [a3 count])
[finalMutableArray addObject:[a3 objectAtIndex:index]];
}
NSArray *finalArray = [NSArray arrayWithArray:finalMutableArray];
[a1 release];
[a2 release];
[a3 release];
NSArray*a1=[[NSArray alloc]initWithObjects:@“猫”,“狗”,“蝙蝠”,无];
NSArray*a2=[[NSArray alloc]initWithObjects:@“Apple”@“Mango”@“Banana”@“Pear”,nil];
NSArray*a3=[[NSArray alloc]initWithObjects:@“大”,“小”,无];
NSU整数数组长度=[a1计数]+[a2计数]+[a3计数];
NSMutableArray*finalMutableArray=[NSMutableArrayWithCapacity:(arrayLength)];
对于(整数索引=0;索引<[a2计数];索引++){//取最大数组的大小
如果(索引<[a1计数])
[finalMutableArray添加对象:[a1对象索引:索引];
[finalMutableArray添加对象:[a2对象索引:索引]];
如果(索引<[a3计数])
[finalMutableArray添加对象:[a3对象索引:索引];
}
NSArray*finalArray=[NSArray数组带数组:finalMutalArray];
[a1释放];
[a2释放];
[a3发布];
填充BigArray背后的逻辑是什么?什么时候使用哪个数组中的哪个元素?我需要通过从每个用户数组中依次选取元素来创建BigArray。为什么选择arrayLength*NumberOfArray?您正在创建一个只包含arrayLength元素的数组。现在看起来好多了。您以前的代码也运行得很好,我只是觉得有点不对劲。@Vaiko:以前对代码的优化不够好。他急于把答案贴出来。
NSArray *a1 = [[NSArray alloc] initWithObjects:@"Cat", @"Dog", @"Bat",nil];
NSArray *a2 = [[NSArray alloc] initWithObjects:@"Apple", @"Mango", @"Banana", @"Pear",nil];
NSArray *a3 = [[NSArray alloc] initWithObjects:@"Big", @"Small",nil];
NSUInteger arrayLength = [a1 count]+[a2 count]+[a3 count];
NSMutableArray *finalMutableArray = [NSMutableArray arrayWithCapacity:(arrayLength)];
for (NSUInteger index = 0; index < [a2 count]; index++) { // take the size of the biggest array
if(index < [a1 count])
[finalMutableArray addObject:[a1 objectAtIndex:index]];
[finalMutableArray addObject:[a2 objectAtIndex:index]];
if(index < [a3 count])
[finalMutableArray addObject:[a3 objectAtIndex:index]];
}
NSArray *finalArray = [NSArray arrayWithArray:finalMutableArray];
[a1 release];
[a2 release];
[a3 release];