Reputation: 2110
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.
Upvotes: 50
Views: 28718
Reputation: 32661
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.
Upvotes: 5
Reputation: 10865
Simply
[array insertObject:obj atIndex:0];
Check the documentation
Upvotes: 111