数组NSArray和可变数组NSMutableArray的基础知识
2015-11-11 20:39
597 查看
数组 NSArray
//创建 NSMutableArray *firstMutableArray = [NSMutableArrayarray]; NSMutableArray *secondMutableArray = [NSMutableArrayarrayWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",nil]; NSMutableArray *fourMutableArray = [NSMutableArrayarrayWithArray:firstArray]; NSMutableArray *sixMutableArray = [NSMutableArrayarrayWithCapacity:10];//创建一个容量为10的数组 NSMutableArray *threeMutableArray = [NSMutableArrayarrayWithContentsOfURL:url]; NSMutableArray *fiveMutableArray = [NSMutableArrayarrayWithContentsOfFile:path]; //添加 add [firstMutableArrayaddObject:@"周六"]; [firstMutableArrayaddObjectsFromArray:secondMutableArray]; //插入 insert //将要插入的元素插入到当前的index位置上,原来在该位置上的元素自动往后移位 [firstMutableArrayinsertObject:@"周日"atIndex:0]; //删除 remove [firstMutableArrayremoveLastObject]; //删除数组内最后一个元素 [firstMutableArrayremoveObject:@"周日"]; //删除数组内指定元素 [firstMutableArrayremoveObjectAtIndex:0]; //删除数组内指定位置的元素 [firstMutableArrayremoveObject:@"周一"inRange:NSMakeRange(1,2)]; //删指定范围内的某个元素 [firstMutableArrayremoveAllObjects]; //删除数组内所有元素(清空) //替换 replace //用特定元素,替换数组内指定位置的元素 [firstMutableArray replaceObjectAtIndex:0withObject:@"礼拜一"]; //用另一个数组的指定范围,替换当前数组的指定范围 [firstMutableArray replaceObjectsInRange:NSMakeRange(0,2)withObjectsFromArray:secondArrayrange:NSMakeRange(2,4)]; //交换 指定位置的两个元素 exchang [firstMutableArray exchangeObjectAtIndex:0withObjectAtIndex:1]; //遍历集合 for (NSString *namein firstMutableArray) { //forin的collection仅可存放:数组,字典,集合 NSLog(@"%@", name); }
可变数组 NSMutableArray
//创建 NSMutableArray *firstMutableArray = [NSMutableArrayarray]; NSMutableArray *secondMutableArray = [NSMutableArrayarrayWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",nil]; NSMutableArray *fourMutableArray = [NSMutableArrayarrayWithArray:firstArray]; NSMutableArray *sixMutableArray = [NSMutableArrayarrayWithCapacity:10];//创建一个容量为10的数组 NSMutableArray *threeMutableArray = [NSMutableArrayarrayWithContentsOfURL:url]; NSMutableArray *fiveMutableArray = [NSMutableArrayarrayWithContentsOfFile:path]; //添加 add [firstMutableArrayaddObject:@"周六"]; [firstMutableArrayaddObjectsFromArray:secondMutableArray]; //插入 insert //将要插入的元素插入到当前的index位置上,原来在该位置上的元素自动往后移位 [firstMutableArrayinsertObject:@"周日"atIndex:0]; //删除 remove [firstMutableArrayremoveLastObject]; //删除数组内最后一个元素 [firstMutableArrayremoveObject:@"周日"]; //删除数组内指定元素 [firstMutableArrayremoveObjectAtIndex:0]; //删除数组内指定位置的元素 [firstMutableArrayremoveObject:@"周一"inRange:NSMakeRange(1,2)]; //删指定范围内的某个元素 [firstMutableArrayremoveAllObjects]; //删除数组内所有元素(清空) //替换 replace //用特定元素,替换数组内指定位置的元素 [firstMutableArray replaceObjectAtIndex:0withObject:@"礼拜一"]; //用另一个数组的指定范围,替换当前数组的指定范围 [firstMutableArray replaceObjectsInRange:NSMakeRange(0,2)withObjectsFromArray:secondArrayrange:NSMakeRange(2,4)]; //交换 指定位置的两个元素 exchang [firstMutableArray exchangeObjectAtIndex:0withObjectAtIndex:1]; //遍历集合 for (NSString *namein firstMutableArray) { //forin的collection仅可存放:数组,字典,集合 NSLog(@"%@", name); }
相关文章推荐
- Spark on Yarn——spark1.5.1集群配置
- 二叉树的最大深度算法面试题-leetcode学习之旅(3)
- Random类(随机数)
- iOS数据库操作(使用FMDB)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十八)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十八)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十八)
- Unity3D-Terrain地形编辑
- 2015-11-11
- Subsets and Subsets II
- 多线程的创建问题
- JAVA jdom解析xml简单用法
- Android 布局优化
- Android逆向之动态调试总结
- T044 二分搜索(含鬼畜压行写法)
- 转:详解JMeter正则表达式(2)
- hdu 3032(博弈)
- Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目教程
- 转:详解JMeter正则表达式(1)
- C++容器