热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

利用NSArrays实现集合的交集与并集操作-ImplementingSetOperationswithNSArrays:IntersectionandUnion

在本文中,我们探讨了如何使用NSArrays来实现集合的交集与并集操作。通过两个示例数组A和B,其中包含一些共同元素(例如A:1,2,3和B:2,3,4),我们将详细介绍如何高效地进行这些集合操作。此外,我们还将讨论这些方法在实际应用中的性能优势和适用场景。

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

我有两个nsarray A和B它们有一些共同的元素。

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.

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

The operation on the previous NSArrays would yield:

对前一次nsarray的操作将产生:

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

How do I accomplish this in Objective-C?

如何在Objective-C中实现?

5 个解决方案

#1


21  

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

将nsarray转换为nsset,可以使用标准的set操作。

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)

NSLog输出:c:(4,5)

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

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

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

#2


15  

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

正如其他人所建议的,您可以轻松地使用NSSet实现这一点。然而,这并不能保持顺序。

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.

如果您想保持排序,并且您可以针对OS X 10.7+,那么您可以使用新的NSOrderedSet(和mutablecsubclass)来做同样的事情。

#3


6  

By using NSSet, as others have pointed out. For

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

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.

这可以解决问题,但不能维持秩序。您将在“set”中获取结果并将其排序到一个数组中。如果您更倾向于保持订单和对dupes的担心,则使用NSMutableArray的-removeObjectsInArray:方法,等等。

#4


1  

(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.

(∩B)∪B将永远给你,这是一个很奇怪的事情要计算。这就像说“给我一套绿色的车,再加上所有的车”。它会给出所有汽车的集合。

#5


0  

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

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


推荐阅读
  • 本文介绍了如何使用Objective-C语言遍历指定文件夹,并根据文件扩展名来判断文件类型的方法。代码示例中通过创建一个文件管理器实例,利用目录枚举器遍历文件夹中的所有项,筛选出特定类型的文件。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文详细介绍了Objective-C中的面向对象编程概念,重点探讨了类的定义、方法的实现、对象的创建与销毁等内容,旨在帮助开发者更好地理解和应用Objective-C的面向对象特性。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文详细介绍了C语言中链表的两种动态创建方法——头插法和尾插法,包括具体的实现代码和运行示例。通过这些内容,读者可以更好地理解和掌握链表的基本操作。 ... [详细]
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • 在多线程编程环境中,线程之间共享全局变量可能导致数据竞争和不一致性。为了解决这一问题,Linux提供了线程局部存储(TLS),使每个线程可以拥有独立的变量副本,确保线程间的数据隔离与安全。 ... [详细]
  • 本文深入探讨了面向切面编程(AOP)的概念及其在Spring框架中的应用。通过详细解释AOP的核心术语和实现机制,帮助读者理解如何利用AOP提高代码的可维护性和开发效率。 ... [详细]
  • 本文介绍了iOS应用开发的主要框架,包括Foundation、UIKit、CoreData及CoreGraphics等,并探讨了开发iOS应用所需的硬件和软件环境,以及推荐的编程语言。 ... [详细]
author-avatar
你有小号我就不能有吗_477
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有