0

我想比较 2 个 NSMutableArray 值并检查哪个 indexPath 不同并将这些索引数存储在一个数组中。例如我有这些 NSMutableArray :

NSMutableArray *a = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
NSMutableArray *b = [[NSMutableArray alloc]initWithObjects:@"11",@"2",@"5",@"3",@"53",@"6", nil];

我想将这两个 NSMutableArray 索引与索引进行比较(将值与值进行比较)并告诉我这些 NSMutableArrays 中数组的哪个索引不同。例如,在顶部的两个 NSMutableArray 中,此索引不同(0,2,3,4),我想将此值存储在某个数组中。

4

2 回答 2

0
NSMutableArray* differentIndex;
differentIndex = [[NSMutableArray alloc] init];
for(int i = 0 ; i < [a count]; i++) {
    if(i < [b count]){
        if(![b[i] isEqual:a[i]]){
             [differentIndex addObject:[NSNumber numberWithInt:i]];
         }
    } else {
        [differentIndex addObject:[NSNumber numberWithInt:i]];
    }
}

现在您的 differentIndex 包含数组中的所有不同索引。:)

于 2013-05-29T13:15:42.993 回答
0
NSMutableArray *a = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
NSMutableArray *b = [[NSMutableArray alloc]initWithObjects:@"11",@"2",@"5",@"3",@"53",@"6", nil];
NSMutableArray *new = [[NSMutableArray alloc]init];
for(int i =0;i<[b count]<=[a count]?[b count]:[a count];i++)
{
    NSString *_keyA = [a objectAtIndex:i];
    NSString *_keyB = [b objectAtIndex:i];
    if ([_keyA isEqualToString:_keyB])
       continue;
    [new addObject:[NSString stringWithFormat:@"%d",i]];
}
于 2013-05-29T13:23:04.663 回答