可变数组NSMutableArray

来源:互联网 发布:charles 修改请求数据 编辑:程序博客网 时间:2024/05/19 02:25

//NSMutableArray创建一个可变数组

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

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

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

        NSMutableArray *mulArray = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];

        NSLog(@"mulArray:%@",mulArray);        

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

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

        [mulArray addObject:@"Five"];

        NSLog(@"newArray=%@",mulArray);

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

        //(void)insertObject:(id)anObject atIndex:(NSUInteger)index;        

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

        //删除一个传递的对象

        //(void)removeLastObject;      

        [mulArray removeObject:@"qianfeng"];

        NSLog(@"newArray=%@",mulArray);  

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

        //(void)removeObjectAtIndex:(NSUInteger)index;        

        [mulArray removeObjectAtIndex:2];

        NSLog(@"newArray=%@",mulArray);       

        //修改可变数组

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

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

        NSLog(@"newArray=%@",mulArray);       

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

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

        NSMutableArray *newArray = @[@"aha",@"hehe"];

        [mulArray addObjectsFromArray:newArray];

        NSLog(@"newArray=%@",mulArray)    

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

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

        [mulArray exchangeObjectAtIndex:1 withObjectAtIndex:3];

        NSLog(@"newArray=%@",mulArray);   

        //删除所有的对象

        //(void)removeAllObjects;       

        [mulArray removeAllObjects];

        NSLog(@"newArray=%@",mulArray);        

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

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

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

        NSLog(@"newArray=%@",mulArray);       

        //删除所有数组中传递的对象

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

        [mulArray removeObject:@"hehe"];

        NSLog(@"newArray=%@",mulArray);        

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

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

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

        [mulArray removeObjectsInArray:mul2];

        NSLog(@"newArray=%@",mulArray);        

        //(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)setArray:(NSArray *)otherArray;        

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

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

        NSIndexSet *inSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(12)];

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

        NSLog(@"mul3=%@",mul3);        

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

        NSMutableArray *mulArr = [NSMutableArray arrayWithCapacity:20];        

        //(void)removeObjectsAtIndexes:(NSIndexSet *)indexes;               

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

0 0