通过交集和并集组合 NSArray

发布于 2024-12-07 11:57:52 字数 314 浏览 1 评论 0原文

我有两个共享一些公共元素的 NSArray A 和 B,例如,

A: 1,2,3,4,5 
B: 4,5,6,7

我想创建一个新的 NSArray,其中包含两个 NSArray 之间的公共内容,并与第二个 NSArray 的内容相连接,同时保持元素的顺序并删除重复项。也就是说,我想要 (A ∩ B) ∪ B。

对先前 NSArray 的操作将产生:

A ∩ B: 4,5
(A ∩ B) ∪ B: 4,5,6,7

如何在 Objective-C 中完成此操作?

I have two NSArrays A and B that share some common elements, e.g.

A: 1,2,3,4,5 
B: 4,5,6,7

I would like to create a new NSArray consisting of the contents common between the two NSArrays joined with the contents of the second NSArray while maintaining the order of the elements and removing duplicates. That is, I would like (A ∩ B) ∪ B.

The operation on the previous NSArrays would yield:

A ∩ B: 4,5
(A ∩ B) ∪ B: 4,5,6,7

How do I accomplish this in Objective-C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

简单气质女生网名 2024-12-14 11:57:52

NSArray转换为NSSet,可以使用标准的集合操作。

NSArray *a = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
NSArray *b = [NSArray arrayWithObjects:@"4", @"5", @"6", @"7", nil];

NSMutableSet *setA = [NSMutableSet setWithArray:a];
NSSet *setB = [NSSet setWithArray:b];
[setA intersectSet:setB];
NSLog(@"c: %@", [setA allObjects]);

NSLog 输出:c: (4, 5)

[setA unionSet:setB];
NSLog(@"d: %@", [setA allObjects]);

NSLog 输出:d: (6, 4, 7, 5)

Convert the NSArrays to NSSets, the standard set operations are available.

NSArray *a = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
NSArray *b = [NSArray arrayWithObjects:@"4", @"5", @"6", @"7", nil];

NSMutableSet *setA = [NSMutableSet setWithArray:a];
NSSet *setB = [NSSet setWithArray:b];
[setA intersectSet:setB];
NSLog(@"c: %@", [setA allObjects]);

NSLog output: c: (4, 5)

[setA unionSet:setB];
NSLog(@"d: %@", [setA allObjects]);

NSLog output: d: (6, 4, 7, 5)

鹿! 2024-12-14 11:57:52

正如其他人所建议的,您可以使用 NSSet 轻松完成此操作。但是,这不会保留顺序。

如果您想保留顺序并且可以面向 OS X 10.7+,那么您可以使用新的 NSOrderedSet (和可变子类)做同样的事情。

As others have suggested, you can easily do this with NSSet. However, this will not preserve ordering.

If you want to preserve ordering and you can target OS X 10.7+, then you can use the new NSOrderedSet (and mutable subclass) to do the same thing.

柠檬色的秋千 2024-12-14 11:57:52

正如其他人指出的那样,通过使用 NSSet。因为

NSArray * a = [NSArray arrayWithObjects: ... ];
NSArray * b = [NSArray arratWithObjects: ... ];
NSMutableSet * set = [NSMutableSet setWithArray:a];
[set intersectSet:[NSSet setWithArray:b];
[set unionSet:[NSSet setWithArray:b];

这可以解决欺骗问题,但不会维持秩序。您将获取“set”中的结果并将它们重新排序到数组中。没有原生的集合功能可以完成这一切——如果您希望保持顺序并单独担心重复,请使用 NSMutableArray 的 -removeObjectsInArray: 方法等。

By using NSSet, as others have pointed out. For

NSArray * a = [NSArray arrayWithObjects: ... ];
NSArray * b = [NSArray arratWithObjects: ... ];
NSMutableSet * set = [NSMutableSet setWithArray:a];
[set intersectSet:[NSSet setWithArray:b];
[set unionSet:[NSSet setWithArray:b];

This takes care of dupes but won't preserve order. You'd take the results in "set" and sort them back into an array. There's no native collection functionality that will do it all-- if you prefer to keep the order and worry about dupes separately, use NSMutableArray's -removeObjectsInArray: method, etc.

老街孤人 2024-12-14 11:57:52

(A ∩ B) ∪ B 总会给你 B,所以这是一个计算起来相当奇怪的事情。这就像说“给我所有绿色汽车的集合,以及所有汽车的集合”。这将为您提供所有汽车的集合。

(A ∩ B) ∪ B will always give you B, so this is a pretty bizarre thing to want to calculate. It's like saying "Give me the set of all cars that are colored green, combined with the set of all cars". That's going to give you the set of all cars.

隐诗 2024-12-14 11:57:52

您可以使用一个 NSSet 类来执行这些操作。

There is an NSSet class you can use to perform these operations.

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