简体   繁体   中英

Add an object to the beginning of an NSMutableArray?

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.

Simply

[array insertObject:obj atIndex:0];

Check the documentation

As other answers have noted just use the insertObject:atIndex method. It is efficient as NSArrays do not necessarily consist of contiguous memory ie the elements don't always get moved when the insert happens especially for large arrays ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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