Objective C 等价于 Java 的 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3354134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:27:16  来源:igfitidea点击:

Objective C equivalent to Java's ArrayList

javaobjective-carraylistequivalent

提问by Julio

Just wondering if there is an equivalent to Java's ArrayList in Objective-C?

只是想知道在 Objective-C 中是否有与 Java 的 ArrayList 等效的东西?

Or something that can be used to store objects/data that does not have a set size.

或者可以用来存储没有设定大小的对象/数据的东西。

Thanks.

谢谢。

回答by Barry Wark

As others have pointed out, NSArray/NSMutableArrayare what you're looking for.

正如其他人指出的那样,NSArray/ NSMutableArray正是您要寻找的。

Coming from the Java world, you may find that Cocoa's collection offerings feel quite paltry. In fact, the functionality is quite extensive. The NSArray, NSDictionary, and NSSetare, in fact, class clusters, meaning that the public API is an "abstract" class. When you initialize one of the collections, what you get back is, in fact, a concrete implementation tailored for the data you provide. These implementations can also change the concrete implementation at run time if the data changes (e.g. it grows in size). This is all possible due to Objective-C's much more dynamic run time than Java's static typing (and security) will allow. The class cluster strategy thus hides many of the implementations of, e.g. the java.util.Listinterface, behind a single API.

来自 Java 世界,您可能会发现 Cocoa 的集合产品感觉非常微不足道。事实上,功能相当广泛。的NSArrayNSDictionaryNSSet,事实上,类集群,这意味着公共API是一个“抽象”类。当您初始化其中一个集合时,您得到的实际上是为您提供的数据量身定制的具体实现。如果数据发生变化(例如它的大小增长),这些实现还可以在运行时更改具体实现。这一切都是可能的,因为 Objective-C 的动态运行时间比 Java 的静态类型(和安全性)所允许的要多得多。因此,类集群策略将许多实现(例如java.util.List接口)隐藏在单个 API 后面。

The Cocoa frameworks are somewhat limited in compound data structures (i.e. those built on top of "primitive" arrays, sets, and dictionaries). You may find that the excellent, open source CHDataStructuresfills in many of the gaps.

Cocoa 框架在复合数据结构(即建立在“原始”数组、集合和字典之上的那些)方面有些受限。您可能会发现优秀的开源CHDataStructures填补了许多空白。

回答by mipadi

NSMutableArray. You can add objects to it as needed.

NSMutableArray. 您可以根据需要向其中添加对象。