黑马程序员——Foundation---NSMutableArray


------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------




NSMutableArray是可变数组类,是NSArray的子类。可变既代表着可对数组元素进行增、删、改操作。


创建:

<span style="font-family:SimHei;font-size:18px;">//创建未指定长度的空数组
NSMutableArray *arr1 = [NSMutableArray array];
//创建一个指定长度为10的空数组
NSMutableArray *arr2 = [[NSMutableArray alloc] initWithCapacity:10];
//创建一个数组,包含若干元素
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@1,@2,@3,nil];
//调用对象方法创建包含若干元素的数组
NSMutableArray *arr4 = [[NSMutableArray alloc] initWithObjects:@1,@2,@3,nil];</span>

遍历:

<span style="font-family:SimHei;font-size:18px;">//普通for循环遍历
for (int i=0; i<arr4.count; i++) {
    NSLog(@"%@",arr4[i]);
}
//增强型forin循环遍历
for (NSNumber *num in arr4) {
    NSLog(@"%@",num);
}
//结构体遍历
[arr4 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    NSLog(@"%@",obj);
}];</span>

以上三种方式输出均为

<span style="font-family:SimHei;font-size:18px;">2015-10-11 23:01:51.214 block[1647:271205] 1
2015-10-11 23:01:51.215 block[1647:271205] 2
2015-10-11 23:01:51.215 block[1647:271205] 3
Program ended with exit code: 0</span>

增:

<span style="font-family:SimHei;font-size:18px;">//在数组末尾添加一个元素
- (void)addObject:(id)object;
[arr4 addObject:@4];
//在下标为NSUinteger的位置插入对象id
-(void)insertObject:(id) atIndex:(NSUInteger);
[arr4 insertObject:@9 atIndex:2];</span>
遍历输出结果为

<span style="font-family:SimHei;font-size:18px;">2015-10-11 23:10:20.482 block[1683:277561] 1
2015-10-11 23:10:20.483 block[1683:277561] 2
2015-10-11 23:10:20.483 block[1683:277561] 9
2015-10-11 23:10:20.483 block[1683:277561] 3
2015-10-11 23:10:20.483 block[1683:277561] 4
Program ended with exit code: 0</span>

删:

<span style="font-family:SimHei;font-size:18px;">//删除index位置的元素
- (void)removeObjectAtIndex:(NSUInteger)index;
[arr4 removeObjectAtIndex:2];
</span>
输出结果为
<span style="font-family:SimHei;font-size:18px;">2015-10-11 23:15:58.265 block[1691:281330] 1
2015-10-11 23:15:58.265 block[1691:281330] 2
2015-10-11 23:15:58.266 block[1691:281330] 3
2015-10-11 23:15:58.266 block[1691:281330] 4
Program ended with exit code: 0</span>

<span style="font-family:SimHei;font-size:18px;">//删除特定的元素(有一个删一个,有两个删一双)
(void)removeObject:(id)object;
NSMutableArray *arr5 = [NSMutableArray arrayWithArray:@[@1,@2,@3,@4,@2,@6,@7,@2]];
[arr5 removeObject:@2];</span>

输出结果为

<span style="font-family:SimHei;font-size:18px;">2015-10-11 23:27:14.996 block[1753:288274] 1
2015-10-11 23:27:14.996 block[1753:288274] 3
2015-10-11 23:27:14.996 block[1753:288274] 4
2015-10-11 23:27:14.996 block[1753:288274] 6
2015-10-11 23:27:14.997 block[1753:288274] 7
Program ended with exit code: 0
</span>


<span style="font-family:SimHei;font-size:18px;">//删除range范围内的所有元素,不在此范围则无视
- (void)removeObjectsInRange:(NSRange)range;
NSMutableArray *arr5 = [NSMutableArray arrayWithArray:@[@1,@2,@3,@4,@5,@6,@7,@8]];
NSRange r = {2,3};
[arr5 removeObjectsInRange:r];</span>

输出结果为

<span style="font-family:SimHei;font-size:18px;">2015-10-11 23:33:08.021 block[1782:292450] 1
2015-10-11 23:33:08.022 block[1782:292450] 2
2015-10-11 23:33:08.022 block[1782:292450] 6
2015-10-11 23:33:08.022 block[1782:292450] 7
2015-10-11 23:33:08.023 block[1782:292450] 8
Program ended with exit code: 0</span>


改:
<span style="font-family:SimHei;font-size:18px;">//用anObject替换index位置对应的元素
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
NSMutableArray *arr5 = [NSMutableArray arrayWithArray:@[@1,@2,@3,@4,@5,@6,@7,@8]];
//[arr5 replaceObjectAtIndex:3 withObject:@10];此句与arr5[3]=@10;相同
arr5[3]=@10;</span>

输出结果为

<span style="font-family:SimHei;font-size:18px;">2015-10-11 23:34:11.148 block[1791:293316] 1
2015-10-11 23:34:11.149 block[1791:293316] 2
2015-10-11 23:34:11.149 block[1791:293316] 3
2015-10-11 23:34:11.149 block[1791:293316] 10
2015-10-11 23:34:11.149 block[1791:293316] 5
2015-10-11 23:34:11.149 block[1791:293316] 6
2015-10-11 23:34:11.149 block[1791:293316] 7</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值