ios 将对象添加到 NSMutableArray 的开头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7292568/
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
Add an object to the beginning of an NSMutableArray?
提问by gurooj
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.
有没有一种有效的方法可以将对象添加到 的开头NSMutableArray
?我正在寻找一个好的双端队列objective C
也可以工作。
回答by Manlio
回答by user151019
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 blogAlso 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.
正如其他答案所指出的那样,只需使用该insertObject:atIndex
方法。它是有效的,因为 NSArrays 不一定由连续的内存组成,即当插入发生时元素并不总是被移动,特别是对于大数组,即几十万个元素。请参阅此博客另请注意,在目标 C 中,只有指针在数组中移动,因此 memmove 可以在内部使用,而 C++ 则必须进行复制。
Also this SE question.
还有这个 SE 问题。