0

如果我有 3 个 NSMutableArray,如何获得一个数组,其中每个索引处的元素是每个原始数组中同一索引处的元素之和?

例子:

MutableArrayOne  = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

MutableArrayTwo  = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

MutableArrayThree  = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

我如何将它们加到一个数组中,例如:

MutableArrayThree  = [NSMutableArray arrayWithObjects:@"3",@"6",@"9",@"12",@"15", nil];
4

1 回答 1

3

假设所有数组的大小相同......

NSMutableArray *sums = [NSMutableArray arrayWithCapacity:MutableArrayOne.count];
for(NSInteger i = 0; i < MutableArrayOne.count; i++)
{
    NSInteger element1 = [[MutableArrayOne objectAtIndex:i] integerValue];
    NSInteger element2 = [[MutableArrayTwo objectAtIndex:i] integerValue];
    NSInteger element3 = [[MutableArrayThree objectAtIndex:i] integerValue];
    NSInteger sum = element1 + element2 + element3;
    [sums addObject:[NSString stringWithFormat:@"%lu", sum]];
}
于 2012-08-10T22:17:12.487 回答