从一个数组中查找另一个数组中的 NSString(for 循环和 isEqualToString 问题)
我想添加 NSArray *small 中的对象,只要它们不存在于 NSArray *big 中。 但输出显示*big 中存在的*small 中的对象已被添加。我已经尝试过 x isEqualToString: x == NO
(我知道它与 range.location 和 hasPrefix 不同,但奇怪的是,即使 hiya -> 当它确实存在于 *big 中时也会添加) 和range.location = NSNotFound
和x hasPrefix:x == NO
。但它们都不起作用。为什么?
顺便说一下,*small 包含的对象比 *big 少。有关系吗?
下面的代码:
NSArray *big = [[NSArray alloc] initWithObjects:@"hello ->hi", @"hiya ->", @"hiya ->whatever", @"hiya -> howdy", @"good day ->hello", @"nope, but ->no", @"however ->what", @"May ->april", @"mai ->", nil];
NSArray *small = [[NSArray alloc] initWithObjects: @"match", @"hiya ->",@"hiya ->", @"hiya ->",@"nope, but ->", @"however ->", @"May ->", nil];
NSString *same;
NSMutableArray *newWords = [[NSMutableArray alloc]init];
newWords = [NSMutableArray arrayWithArray: big];
NSLog (@"big: %@", big);
int i;
for (i = 0; i<[small count]; i++)
{
same = [small objectAtIndex:i];
for (NSString *s in big)
{
//NSRange ran = [s rangeOfString:same];
//if (ran.location =NSNotFound)
//if ([s isEqualToString: same] == NO)
if ([s hasPrefix:same] == NO)
{
[newWords addObject:same];
break;
}
}
}
输出显示:
2011-10-17 19:21:56.855 scanner2[4018:207] newWords: (
"hello ->hi",
"hiya ->",
"hiya ->whatever",
"hiya -> howdy",
"good day ->hello",
"nope, but ->no",
"however ->what",
"May ->april",
"mai ->",
match,
"hiya ->",
"hiya ->",
"hiya ->",
"nope, but ->",
"however ->",
"May ->"
)
编辑:我什至尝试了 if ([x Compare: x ] !=NSOrderedSame)
,但只添加了三次 hiya ->
*新词。
I want to add objects from NSArray *small whenever they don´t exist in NSArray *big.
But the output shows that the objects from *small which exist in *big are added. I have tried x isEqualToString: x == NO
(I know it is not same as range.location and hasPrefix, but it is weird that even hiya -> is added when it does exist in *big)
and range.location = NSNotFound
and x hasPrefix: x == NO
. But neither of them does work. Why?
By the way, *small contains fewer objects than *big. Does it matter?
The codes below:
NSArray *big = [[NSArray alloc] initWithObjects:@"hello ->hi", @"hiya ->", @"hiya ->whatever", @"hiya -> howdy", @"good day ->hello", @"nope, but ->no", @"however ->what", @"May ->april", @"mai ->", nil];
NSArray *small = [[NSArray alloc] initWithObjects: @"match", @"hiya ->",@"hiya ->", @"hiya ->",@"nope, but ->", @"however ->", @"May ->", nil];
NSString *same;
NSMutableArray *newWords = [[NSMutableArray alloc]init];
newWords = [NSMutableArray arrayWithArray: big];
NSLog (@"big: %@", big);
int i;
for (i = 0; i<[small count]; i++)
{
same = [small objectAtIndex:i];
for (NSString *s in big)
{
//NSRange ran = [s rangeOfString:same];
//if (ran.location =NSNotFound)
//if ([s isEqualToString: same] == NO)
if ([s hasPrefix:same] == NO)
{
[newWords addObject:same];
break;
}
}
}
The output shows:
2011-10-17 19:21:56.855 scanner2[4018:207] newWords: (
"hello ->hi",
"hiya ->",
"hiya ->whatever",
"hiya -> howdy",
"good day ->hello",
"nope, but ->no",
"however ->what",
"May ->april",
"mai ->",
match,
"hiya ->",
"hiya ->",
"hiya ->",
"nope, but ->",
"however ->",
"May ->"
)
edit: I even tried if ([x compare: x ] !=NSOrderedSame)
, but only hiya ->
is added thrice to *newWords.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在将 Small 中的每个字符串与 Big 中的每个字符串进行比较。如果 Big 中的任何字符串与您要比较的字符串不同,则将其添加到 Big 中。但实际上,如果所有字符串都不相同,您需要添加它。所以,你需要等到检查完整个大数组之后。
试试这个:
为了迂腐,我应该补充一点,如果数组比您显示的演示字符串大得多,那么您可能需要将 Big 维护为排序数组,以便您可以找到存在或不存在快得多。
例如:
显然,如果您多次执行此操作,只需将大小保持为已排序的可变数组,而不是每次都重新排序。
You are comparing each string in Small to each string in Big. If ANY of the strings in Big are not the same as the one you're comparing, then you're adding it to Big. But actually, you want to add it if ALL of the strings are not the same. So, you need to wait until after checking the whole big array.
Try this instead:
To be pedantic, I should add that if the arrays are going to be MUCH bigger than the demo strings you're showing, then you might want to maintain Big as a sorted array, so that you can find presence or absence much faster.
For example:
Obviously if you're doing this multiple times, just keep big as a sorted mutable array, rather than re-sorting it each time.
你的逻辑很糟糕。您应该结束整个循环来添加元素,您添加得太早了。您需要检查 big 中的每个人都没有这个前缀(不是其中一些)。
Your logic is bad. You should end the full loop to add the element, you are adding it too soon. You need to check that everyone in big has not this prefix (not some of them).