OC_数组
2015-07-21 19:04
302 查看
快速枚举: 能快速的遍历数组等容器对象
区别
可变数组
可变数组的定义
可变数组的一些操作
例题
1.都是对容器里的每一个元素的遍历 2.为了增加代码的阅读性,避免不必要的错误,尽量让forin的前部分的类型和数组里的元素类型相同
NSArray *arr = @[@"杨林", @"刘鑫琪", @"李牧燃", @"腾飞",]; for (NSString * str in arr) { NSLog(@"%@", str); }
NSArray *arr1 = @[@"杨林", @"刘鑫琪", @"李牧燃", @"腾飞",]; NSArray *arr2 = @[@"李珊珊", @"郭鸿瑞", @"何岸", @"流星雨"]; NSArray *arr = @[arr1, arr2]; //对arr进行forin遍历 for (NSArray *temp in arr) { for (NSString *str in temp) { NSLog(@"%@", str); } }
Student *stu1 = [[Student alloc] initWithName:@"李牧燃"]; Student *stu2 = [[Student alloc] initWithName:@"刘鑫琪"]; Student *stu3 = [[Student alloc] initWithName:@"刘山山"]; Student *stu4 = [[Student alloc] initWithName:@"腾飞"]; NSArray *arr1 = @[stu1, stu2, stu3]; NSArray *arr2 = @[stu4]; NSArray *arr = @[arr1 ,arr2]; //便利下数组里每一个学生的名字 for (NSArray *temp in arr) { for (Student *stu in temp) { NSLog(@"%@", stu.name); } }
区别
NSArray *testArr = @[@"1", @"2", @"3"]; // 赋值 NSArray *arr = [[NSArray alloc]initWithArray:testArr]; // 作为一个元素 NSArray *arr1 = [[NSArray alloc] initWithObjects:testArr, nil];
可变数组
可变数组的定义
// 空的可变数组 NSMutableArray *arr = [[NSMutableArray alloc] init]; NSMutableArray *arr1 = [NSMutableArray array]; // 非空可变数组 NSMutableArray *arr2 = [[NSMutableArray alloc] initWithObjects:@"李", @"珊", @"珊", @"傻", @"傻", nil]; NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"李", @"珊", @"珊", @"傻", @"傻", nil];
可变数组的一些操作
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"李", @"珊", @"珊", @"傻", @"傻", nil]; // 增加一个字符串 其结果是添加到数组的最后一位 [arr3 addObject:@"瓜"]; for (NSString * str in arr3) { NSLog(@"%@", str); } // 移除指定下标的字符串(例如:下标3) [arr3 removeObjectAtIndex:3]; for (NSString * str in arr3) { NSLog(@"%@", str); } // 移除指定的字符串 (移除所有相同的字符串) [arr3 removeObject:@"傻"]; for (NSString *temp in arr3) { NSLog(@"%@", temp); } // 指定下标插入一个字符串(例如:下标3) [arr3 insertObject:@"大" atIndex:3]; for (NSString * str in arr3) { NSLog(@"%@", str); } // 替换一个字符串 [arr3 replaceObjectAtIndex:0 withObject:@"刘"]; for (NSString * str in arr3) { NSLog(@"%@", str); } // 交换两个字符串 [arr3 exchangeObjectAtIndex:0 withObjectAtIndex:3]; for (NSString * str in arr) { NSLog(@"%@", str); } // 清空数组 [arr3 removeAllObjects];
例题
Book *book1 = [Book bookWithName:@"西游记" price:30]; Book *book2 = [Book bookWithName:@"红楼梦" price:85]; Book *book3 = [Book bookWithName:@"三国演义" price:100]; Book *book4 = [Book bookWithName:@"水浒传" price:71]; // 四本书放到可变的数组里 NSMutableArray *book = [NSMutableArray arrayWithObjects:book1, book2, book3, book4, nil]; // 对数组进行添加操作 Book *book5 = [Book bookWithName:@"新华字典" price:88]; [book addObject:book5]; for (Book * temp in book) { NSLog(@"%@", temp.name); } // 对数组进行删除操作 [book removeObjectAtIndex:2]; for (Book * temp in book) { NSLog(@"%@", temp.name); } // 在数组里根据书名查找对应的书,并且对书的价钱进行修改 for (Book *temp in book) { if ([temp.name isEqualToString:@"水浒传"]) { temp.price = 98.7; } } for (Book * temp in book) { NSLog(@"%@, %g", temp.name, temp.price); } // 找到价钱在70 到 100 有多少本书, 然后把符合条件的书放在一个数组里 NSMutableArray *newBook = [NSMutableArray array]; NSInteger num = 0; for (Book *temp in book) { if (temp.price >= 70 && temp.price <= 100) { num++; [newBook addObject:temp]; } } NSLog(@"%ld", num); for (Book *temp in newBook) { NSLog(@"%@ %g", temp.name, temp.price); } // 找到价格是100的书,把书名改成论语 for (Book *temp in book) { if (temp.price == 100) { temp.name = @"论语"; } } for (Book *temp in book) { NSLog(@"%@ %g", temp.name, temp.price); }
相关文章推荐
- iOS开发 ----- C小型学生信息管理系统,单向链表
- 【线段树】 HDU 5289 Assignment
- HD2026 首字母变大写
- Java中的private、protected、public和default的区别
- ffmpeg关于avformat_write_header问题
- dup , dup2 , fcntl锁
- VIM
- 那些成功学和鸡汤文不会告诉你的
- 闭包
- hdoj1020
- window8.1编译hadoop2 eclipse插件,并远程调试hadoop程序
- 【瞎搞】HDU 5288 OO’s Sequence
- Java 二分法查找算法
- HTML5 链接记录
- 单电机板机模型,f22
- 单电机板机模型
- 用java 写 merge sort
- 不要62 (暴力打表)
- 目录条目”SASHELP.EMCREDIT.IGN.SOURCE”不存在
- java 二叉树的查找方法