从一个数组中查找另一个数组中的 NSString(for 循环和 isEqualToString 问题)

发布于 2024-12-10 12:44:06 字数 1848 浏览 0 评论 0原文

我想添加 NSArray *small 中的对象,只要它们不存在于 NSArray *big 中。 但输出显示*big 中存在的*small 中的对象已被添加。我已经尝试过 x isEqualToString: x == NO (我知道它与 range.location 和 hasPrefix 不同,但奇怪的是,即使 hiya -> 当它确实存在于 *big 中时也会添加) 和range.location = NSNotFoundx 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-12-17 12:44:06

您正在将 Small 中的每个字符串与 Big 中的每个字符串进行比较。如果 Big 中的任何字符串与您要比较的字符串不同,则将其添加到 Big 中。但实际上,如果所有字符串都不相同,您需要添加它。所以,你需要等到检查完整个大数组之后。

试试这个:

for (NSString *same in small) {
    BOOL found = NO;
    for (NSString *s in big) {
        if ([s hasPrefix:same] == YES) {
            found = YES;
            break;
        }
    }
    if (!found) {
       [newWords addObject:same];
    }
}

为了迂腐,我应该补充一点,如果数组比您显示的演示字符串大得多,那么您可能需要将 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 ->", @"match",nil];

NSDate *start = [NSDate date];

NSMutableArray *newWords = [NSMutableArray  arrayWithArray: big];

for (NSString *same in small) {
    BOOL found = NO;
    for (NSString *s in big) {
        if ([s isEqualToString:same] == YES) {
            found = YES;
            break;
        }
    }
    if (!found) {
        [newWords addObject:same];
    }
}

NSLog(@"Time for original method: %fms ",-[start timeIntervalSinceNow]*1000);

start = [NSDate date];
/*
 static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch;
 NSLocale *currentLocale = [NSLocale currentLocale];
 NSComparator sort = ^(id string1, id string2) {
 NSRange string1Range = NSMakeRange(0, [string1 length]);
 return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
 };*/

NSComparator sort = ^(id s1, id s2) { return [s1 compare:s2]; };

newWords = [NSMutableArray arrayWithArray: 
            [big sortedArrayUsingComparator:sort]]; 

for (NSString *same in small) {
    NSUInteger i = [newWords indexOfObject:same 
                             inSortedRange:NSMakeRange(0, [newWords count]) 
                                   options:NSBinarySearchingInsertionIndex 
                           usingComparator:sort ];
    if (![same isEqualToString:[newWords objectAtIndex:i]] ) {
        [newWords insertObject:same atIndex:i];
    }
}
//    }
NSLog(@"Time for new method: %fms ",-[start timeIntervalSinceNow]*1000);
[big release];
[small release];

显然,如果您多次执行此操作,只需将大小保持为已排序的可变数组,而不是每次都重新排序。

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:

for (NSString *same in small) {
    BOOL found = NO;
    for (NSString *s in big) {
        if ([s hasPrefix:same] == YES) {
            found = YES;
            break;
        }
    }
    if (!found) {
       [newWords addObject:same];
    }
}

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:

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 ->", @"match",nil];

NSDate *start = [NSDate date];

NSMutableArray *newWords = [NSMutableArray  arrayWithArray: big];

for (NSString *same in small) {
    BOOL found = NO;
    for (NSString *s in big) {
        if ([s isEqualToString:same] == YES) {
            found = YES;
            break;
        }
    }
    if (!found) {
        [newWords addObject:same];
    }
}

NSLog(@"Time for original method: %fms ",-[start timeIntervalSinceNow]*1000);

start = [NSDate date];
/*
 static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch;
 NSLocale *currentLocale = [NSLocale currentLocale];
 NSComparator sort = ^(id string1, id string2) {
 NSRange string1Range = NSMakeRange(0, [string1 length]);
 return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
 };*/

NSComparator sort = ^(id s1, id s2) { return [s1 compare:s2]; };

newWords = [NSMutableArray arrayWithArray: 
            [big sortedArrayUsingComparator:sort]]; 

for (NSString *same in small) {
    NSUInteger i = [newWords indexOfObject:same 
                             inSortedRange:NSMakeRange(0, [newWords count]) 
                                   options:NSBinarySearchingInsertionIndex 
                           usingComparator:sort ];
    if (![same isEqualToString:[newWords objectAtIndex:i]] ) {
        [newWords insertObject:same atIndex:i];
    }
}
//    }
NSLog(@"Time for new method: %fms ",-[start timeIntervalSinceNow]*1000);
[big release];
[small release];

Obviously if you're doing this multiple times, just keep big as a sorted mutable array, rather than re-sorting it each time.

雅心素梦 2024-12-17 12:44:06

你的逻辑很糟糕。您应该结束整个循环来添加元素,您添加得太早了。您需要检查 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文