[英]Compare two arrays
I have two arrays: 我有两个数组:
array1=[1,2,3,4,5,6,7,8,9,10,11]
array2=[1,2]
I want to compare weather elements in "array2" is present in "array1" or not. 我想比较“ array1”中是否存在“ array2”中的天气元素。 If yes then I need to run a function, otherwise exit.
如果是,那么我需要运行一个函数,否则退出。 How to do it?
怎么做?
I have get the common items like this:- 我有这样的共同点:
NSMutableSet *idSet=[NSMutableSet setWithArray:Array1];
[idSet intersectSet:[NSSet setWithArray:Array2]];
NSArray *Common_array=[idSet allObjects];
in common array you can get the same object that are present in both array and is 0 object in Common_array than in both array there is none on object that are same. 在公共数组中,您可以获得两个数组中都存在的相同对象,并且Common_array中的对象为0,而在两个数组中都没有相同的对象。
An easy logic way to do this would be a for loop: 一种简单的逻辑方法是for循环:
for(int a = 0; a < array1.count; a++) {
for(int b = 0; b < array2.count; b++) {
if([[array1 objectAtIndex:a] isEqualToString:[array2 objectAtIndex:b]]) {
//do something here
}
}
}
What about enumerating over array1? 枚举array1怎么样?
Something along the lines of 遵循以下原则
NSArray *array1 = ...;
NSArray *array2 = ...;
[array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([array2 containsObject: obj]) {
// Run the function you wanted to
}
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.