在arr3
中,arr1
时我想要arr1[k]==arr2[m]
对象的索引。
但是,我没有得到arr3
中的值,因为它必须不满足条件。
如何通过索引比较两个NSMutableArray对象?
while (k<=[arr1 count]-1 ) {
for (m=0; m<=[arr2 count]-1; m++) {
if ([arr1 objectAtIndex:k]==[arr2 objectAtIndex:m]) {
[arr3 addObject:k];
}
}
k++;
}
答案 0 :(得分:2)
NSMutableArray *arr1,*arr2,*arr3;
arr1=[[NSMutableArray alloc] initWithObjects:@"vijay",@"india",@"AppleVijay@facebook.com",nil];
arr2=[[NSMutableArray alloc] initWithObjects:@"vijay",@"AppleVijay@facebook.com",nil];
arr3=[[NSMutableArray alloc] init];
//Note: Here u have to properly create those arr1,arr2,arr3 arrays with alloc and init and also add objects for arr1,arr2 for compare.
for (id obj in arr2) {//each obj in arr2
if ([arr1 containsObject:obj]) {//if arr1 has the same obj(which is from arr2)
[arr3 addObject:[NSNumber numberWithInteger:[arr1 indexOfObject:obj]]];//then add that indexofobject to arr3
}
}
NSLog(@"resulted arr3 : %@ \n\n",arr3);
<强>输出:强>
resulted arr3 : (
0,
2
)