2011-08-09 47 views
2

arr3我想从arr1arr1[k]==arr2[m]对象的索引。如何比较一个NSMutableArray的每个对象与其他

但是,我没有得到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++; 
} 

回答

2
NSMutableArray *arr1,*arr2,*arr3; 

arr1=[[NSMutableArray alloc] initWithObjects:@"vijay",@"india",@"[email protected]",nil]; 

arr2=[[NSMutableArray alloc] initWithObjects:@"vijay",@"[email protected]",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); 

OUTPUT:

resulted arr3 : (

       0, 

       2 

       ) 
相关问题