将一个对象添加到 NSMutableArray 的开头?

发布于 2024-12-02 22:18:41 字数 91 浏览 7 评论 0原文

有没有一种有效的方法可以将对象添加到 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 技术交流群。

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

发布评论

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

评论(2

时光与爱终年不遇 2024-12-09 22:18:41

只需

[array insertObject:obj atIndex:0];

检查 文档

Simply

[array insertObject:obj atIndex:0];

Check the documentation

淡写薰衣草的香 2024-12-09 22:18:41

正如其他答案所指出的,只需使用 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.

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