2011-08-09 96 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 

       ) 
相關問題