黑马程序员-NSMutableArray的简单使用
在写NSArray的文章中,我们简单提到 NSMutableArray.相比较于其父类 NSArray 而言, NSMutableArray的自大优点就是,初始化之后,仍然可以对数组进行添加元素.并且还可以对数组进行操作.
一.创建
他的创建和NSArray 差不多.上一篇文章中,NSArray 和 NSMutableArray还都可以用[NSArray array]来创建.
二.添加元素
(1)addObject: 添加新元素
(2)addObjectsFromArrays:(NSArray *)otherArray: 把另一个otherArray数组中的元素都添加进来
(3)insertObject:(id) adIndex:(NSInterger) index: 在 index 位置插入一个元素
(4)insertObjects:(NSArray *) adIndex:(NSIndexSet *) indexes: 在 indexes指定的位置分别插入 objects 中的元素
三.删除元素
(1)删除全部: removerAllObjects
(2)删除最后一个元素: removeLastObject
(3)删除 index 位置的元素: removeObjectAtIndex:(NSUInteger *)index
(4)删除 indexes 位置的所有的元素: removeObjectAtIndex:(NSIndexSet *)indexes
(5)删除特定的元素: removeObject:(id)atObject
(6)在 range范围内查找特定的元素进行删除: removeObject:(id)atObject inRange:(NSRange)range
(7)删除和 otherArray 和当前集合中的所有元素: removeObjectsInArray:(NSArray *)otherArray
(8)在 range范围内的所有元素: removeObjectsInRange:(NSRange)range
四.替换和交换元素
(1)replaceObjectArIndex:(NSUInteger)index withObject:(id)anObject 用anObject替换 index 位置对应的元素
(2)exchangeObjectAtIndex:(NSUInterger)idx1 withObjectAtINdex:(NSUInteger)idx2 交换 idx1和 idx2位置的元素
五.排序
具体用法参考 NSArray
浙公网安备 33010602011771号