NSMutableArray类方法整理

来源:互联网 发布:傲骨贤妻 知乎 编辑:程序博客网 时间:2024/05/29 03:28

//NSMutableArray创建一个可变数组

//数组中的成员可以修改

//对象取决于定义对象本身的数据结构

//继承与不可变数组,不可变数组中的所有方法可变数组都可以使用


//********************************增加/删除对象**********************************

//在创建mulArray的时候同时设定数组容纳对象的个数,容器大小可以被改变

NSMutableArray *mulArr = [NSMutableArray arrayWithCapacity:20];

//增加对象,在数组的末尾

[mulArray addObject:@"Five”];

//在数组的指定位置添加对象

[mulArray insertObject:@"qianfeng" atIndex:2];


//传递一个位置删除指定的对象

[mulArray removeObjectAtIndex:2];


//在可变数组末尾添加指定数组

//(void)addObjectsFromArray:(NSArray *)otherArray;

[mulArray addObjectsFromArray:newArray];


//通过另外一个数组来建立数组

(void)setArray:(NSArray *)otherArray;

        

//(void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;

//1是在目的数组中要插入的位置,2是传递插入数据的长度

NSIndexSet *inSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)];

[mul3 insertObjects:@[@"qian",@"feng"] atIndexes:inSet];


//删除一个传递的对象

[mulArray removeObject:@"qianfeng”];


(void)removeObjectsAtIndexes:(NSIndexSet *)indexes;


//删除所有的对象

//(void)removeAllObjects;

mulArray removeAllObjects];


//删除给定范围内传递的对象

//(void)removeObject:(id)anObject inRange:(NSRange)range;

[mulArray removeObject:@"ehe" inRange:NSMakeRange(2, 1)];


//在数组中删除所有包含传递的对象的元素

//(void)removeObject:(id)anObject;

[mulArray removeObject:@"hehe"];


//删除包含传递数组内相同的元素

//(void)removeObjectsInArray:(NSArray *)otherArray;

NSMutableArray *mul2 = @[@"one",@"two"];

[mulArray removeObjectsInArray:mul2];




//********************************修改对象**********************************

//修改可变数组

//(void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

[mulArray replaceObjectAtIndex:withObject:@"helloworld”];


//交换可变数组中指定位置的对象

//(void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;

[mulArray exchangeObjectAtIndex:withObjectAtIndex:3];


//(void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;

NSMutableArray *mul3 = @[@"one",@"two"];

NSMutableArray *mul4 = @[@"one",@"two];

[mul3 replaceObjectsInRange:NSMakeRange(1, 4) withObjectsFromArray:mul4 range:NSMakeRange(1,2)];


(void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;

0 0