将一个对象添加到 NSMutableArray 的开头?
有没有一种有效的方法可以将对象添加到 NSMutableArray 的开头?我正在寻找一个好的双端队列,在objective C
中也可以工作。
Is there an efficient way to add an object to start of an NSMutableArray
? I am looking for a good double ended queue in objective C
would work as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需
检查 文档
Simply
Check the documentation
正如其他答案所指出的,只需使用
insertObject:atIndex
方法。它是高效的,因为 NSArray 不一定由连续的内存组成,即当插入发生时元素并不总是被移动,特别是对于大型数组(即数十万个元素)。请参阅此博客 另请注意,在 Objective C 中仅移动指针在数组中,因此可以在内部使用 memmove,这与必须进行复制的 C++ 不同。还有这个 SE 问题。
As other answers have noted just use the
insertObject:atIndex
method. It is efficient as NSArrays do not necessarily consist of contiguous memory i.e. the elements don't always get moved when the insert happens especially for large arrays i.e. several hundred of thousand elements. See this blog Also note that in objective C only pointers are moved in the array so memmove can be used internally unlike C++ where copies have to be made.Also this SE question.