简体   繁体   English

Objective C相当于Java的ArrayList

[英]Objective C equivalent to Java's ArrayList

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. 谢谢。

As others have pointed out, NSArray / NSMutableArray are what you're looking for. 正如其他人所指出的那样, NSArray / NSMutableArray正是您所寻找的。

Coming from the Java world, you may find that Cocoa's collection offerings feel quite paltry. 来自Java世界,您可能会发现Cocoa的产品系列非常微不足道。 In fact, the functionality is quite extensive. 实际上,功能非常广泛。 The NSArray , NSDictionary , and NSSet are, in fact, class clusters , meaning that the public API is an "abstract" class. 事实上, NSArrayNSDictionaryNSSet类集群 ,这意味着公共API是一个“抽象”类。 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 (eg 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. 这是可能的,因为Objective-C比Java的静态类型(和安全性)允许的动态更加动态。 The class cluster strategy thus hides many of the implementations of, eg the java.util.List interface, behind a single API. 因此,类集群策略隐藏了单个API后面的许多实现,例如java.util.List接口。

The Cocoa frameworks are somewhat limited in compound data structures (ie those built on top of "primitive" arrays, sets, and dictionaries). Cocoa框架在复合数据结构中有些限制(即那些构建在“原始”数组,集合和字典之上的结构)。 You may find that the excellent, open source CHDataStructures fills in many of the gaps. 您可能会发现优秀的开源CHDataStructures填补了许多空白。

NSMutableArray . NSMutableArray You can add objects to it as needed. 您可以根据需要向其添加对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM