------- <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>