简体   繁体   中英

NSMutableArray removeObjectAtIndex

If I have an NSMutableArray with 10 objects

I run this line of code

[tempArray removeObjectAtIndex:0];

then

[tempArray count] should return 9

but does the entire Array shift up

Object At Index 1 moves to Index 0
Object at Index 2 moves to Index 1
...
Object at Index 9 moves to Index 8

or is Index 0 = nil?

From the NSMutableArray documentation :

To fill the gap, all elements beyond index are moved by subtracting 1 from their index.

The array will shift all of the objects down 1 from the right of the removed index. Index 0 will be what was at index 1 and so forth.

removeObjectAtIndex shifts indexes down

insertObjectAtIndex shifts indexed up

addObject adds object at the end so there is no shifting

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