//1.创建一个空的可变数组
NSMutableArray*mArray1 = [[NSMutableArrayalloc]init];
NSMutableArray*mArray2 = [NSMutableArrayarray];
NSLog(@"mArray1 = %@",mArray1);
NSLog(@"mArray2 = %@",mArray2);
//2.把一个不可变的数组转换成可变数组
NSArray*array =@[@"one",@"two",@"three"];
NSMutableArray*mArray3 = [[NSMutableArrayalloc]initWithArray:array];
NSLog(@"mArray3 = %@",mArray3);
NSMutableArray*mArray4 = [NSMutableArrayarrayWithArray:array];
NSLog(@"mArray4 = %@",mArray4);
//3.创建一个默认有多个元素的可变数组
NSMutableArray*mArray5 = [[NSMutableArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
NSMutableArray*mArray6 = [NSMutableArrayarrayWithObjects:@"a",@"b",@"c",@"a",nil];
NSLog(@"mArray5 = %@",mArray5);
NSLog(@"mArray6 = %@",mArray6);
/************增*************/
//1.添加元素(依此往后添加)
[mArray6addObject:@"d"];
NSLog(@"mArray6 = %@",mArray6);
//2.一次性添加多个元素[先把这些元素放入数组中,一次性添加]
NSArray*aa =@[@"SSY",@"2014"];
[mArray6addObjectsFromArray:aa];
NSLog(@"mArray6 = %@",mArray6);
//3.添加元素到指定的索引位置
[mArray6insertObject:@"xyz"atIndex:0];
NSLog(@"mArray6 = %@",mArray6);
/*************删除****************/
//1.删除单个元素
//[mArray6removeObject:@"a"];
//NSLog(@"mArray6 = %@",mArray6);
//2.删除指定范围的元素
NSRangerange = {2,3};
[mArray6removeObject:@"a"inRange:range];
NSLog(@"mArray6 = %@",mArray6);
//3.删除索引位置对的元素
[mArray6removeObjectAtIndex:1];
NSLog(@"mArray6 = %@",mArray6);
//4.删除数组中最后一个元素
[mArray6removeLastObject];
NSLog(@"mArray6 = %@",mArray6);
//5.全部删除
[mArray6removeAllObjects];
NSLog(@"mArray6 = %@",mArray6);
/*************修改**************/
//1.交换数组中两个索引对应的元素
[mArray5exchangeObjectAtIndex:0withObjectAtIndex:mArray5.count-1];
NSLog(@"mArray5 = %@",mArray5);
//2.替换掉指定的索引对应的元素
[mArray5replaceObjectAtIndex:1withObject:@"two"];
NSLog(@"mArray5 = %@",mArray5);
//3.重置数组
[mArray5setArray:@[@"sh",@"1992"]];
NSLog(@"mArray5 = %@",mArray5);
网友评论