简体   繁体   English

比较两个数组并设置if否则

[英]Compare two arrays and set an if else

I need to compare two arrays (A & B), than for the elements of A that belong also to BI need to set an if statement. 我需要比较两个数组(A和B),对于同样属于BI的A元素,需要设置一个if语句。 Just to explain me better: 为了更好地解释我:

if (elementOfArrayA belong AlsoToarrayB) {
        //do something
    }else{
        //do something else
    }

Someone could help me? 有人可以帮我吗? Thanks 谢谢

NSArray has an instance method called containsObject: exactly for this. NSArray有一个名为containsObject:的实例方法。

For further clarification, check this out. 为了进一步澄清,检查出。

Use following code to compare two array : 使用以下代码比较两个数组:

    NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil];
    NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",nil];

     for(int i = 0;i<[array1 count];i++)
        {
            for(int j= 0;j<[array2 count];j++)
            {
                if([[array1 objectAtIndex:i] isEqualToString:[array2 objectAtIndex:j]])
                {

                }  else {

                }
            }
        }

Comparing two arrays: 比较两个数组:

if([arrayA isEqualToArray:arrayB]){
//the two arrays A and B are equals
}

The code above will test ALL elements of both arrays to check if they fulfill the isEqual test, so no need to for loop the array. 上面的代码将测试两个数组的所有元素,以检查它们是否满足isEqual测试,因此无需for循环数组。

If you want to check wether an element of arrayA is contained in arrayB, use the following method: 如果要检查arrayB中是否包含arrayA的元素,请使用以下方法:

id firstCommonObject = [arrayA firstObjectCommonWithArray:arrayB];

if(firstCommonObject != nil){
  //a common object between arrayA and arrayB has been found

}else{
  //no common objects between both arrays
}
// Method 1 - Simplest method to solve above problem (Use NSArray's containsObject method)
NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"e", nil];
NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",@"f", nil];

for(id i in array1){
    if ([array2 containsObject:i]) {
        // do something
    }
    else {
        // do something else
    }
}

// Method 2 - Another method (Use NSString's isEqualToString method)
NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"e", nil];
NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",@"f", nil];

for(id i in array1){
    for(id j in array2){
        if ([i isEqualToString:j]) {
            // do something
        }
        else {
            //do something else
        }
    }
}

it may be help you... 可能对您有帮助...

-(void)methodFour
{
    NSArray *arr1 = [[NSArray alloc]initWithObjects:@"a2223a",@"ab33b",@"a1acdf",@"ac23c45", nil];
    NSArray *arr11 =  [arr1 sortedArrayUsingSelector:@selector(localizedCompare:)];
    NSLog(@"%@",arr11);

    NSArray *arr2 = [[NSArray alloc]initWithObjects:@"ab33b",@"ac23c45",@"a1acdf",@"a2223a", nil];
    NSArray *arr22= [arr2 sortedArrayUsingSelector:@selector(localizedCompare:)];
    [self firstArray:arr11 secondArray:arr22];
   }
-(void)firstArray:(NSArray *)array1 secondArray:(NSArray *)array2
{
    if ([array1 isEqualToArray:array2])
    {
        NSLog(@"equal");

    }
    else
    {
        NSLog(@"Not equal");

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM