集合类Collection
一. 数组
数组:OC 中的数组存储的是对象(数组的局限性), 但是对于对象的类型并没有限制
/**
* 总结: 数组中存放的元素是对象
* 数组存储数据的特点:
* 1. 数组中可以存放相同元素
* 2. 数组存放的数据是有序的
* 3. 数组中可以存放不同的类型的对象
* 数组的使用场景: 当需要一个有序的集合( collection )来管理对象时,就可以使用数组来存储这些对象.
*/
1. NSArray
创建对象
//(1) 初始化方法
NSArray *array = [[NSArray alloc] initWithObjects:@"zhengzhou", @"henan", @"huimian", nil];
NSLog(@"%@", array);
//(2) 便利构造器
NSArray *array1 = [NSArray arrayWithObjects:@"henan", @"zhengzhou", @"fangchenghuimian", nil];
NSLog(@"%@", array1);
数组元素的个数
NSUInteger length = [array count];
NSLog(@"%lu", length);
根据下标访问数组元素
NSString *str1 = [array1 objectAtIndex:2];
NSLog(@"%@", str1);
//(1) 获取第一个元素对象
NSString *str2 = [array1 firstObject];
NSLog(@"str2 = %@", str2);
NSString *str3 = [array1 objectAtIndex:0];
NSLog(@"str3 = %@", str3);
/**
* firstObject 与 ObjectAtIndex:0 的区别:
* 相同点: 都能够获取到第一个元素
* 不同点: 当数组元素个数为零时, 前者不会造成程序崩溃, 返回为 NULL 的对象; 而后者则会引起程序崩溃.
*/
//(2) 获取最后一个元素
NSString * str4 = [array1 lastObject];
NSLog(@"%@", str4);
NSString *str5 = [array1 objectAtIndex:[array1 count] - 1];
NSLog(@"%@", str5);
判断元素是否存在于数组中
BOOL isExist = [array1 containsObject:@"henan"];
if (isExist) {
NSLog(@"真的有河南");
} else {
NSLog(@"河南去哪了");
}
根据数组元素获取该元素的下标
NSUInteger index = [array indexOfObject:@"henan"];
NSLog(@"%lu", index);
遍历
// for (NSUInteger i = 0; i < [array1 count]; i++) {
// NSLog(@"%@", [array1 objectAtIndex:i]);
// }
//快速遍历
//type *object 集合 Collection 中存放的元素的类型
//collection 集合( OC 中的集合, 大容器: 数组, 字典, 集合)
for (NSString *arr in array1) {
NSLog(@"%@", arr);
}
//定义数组, 存储 NSNumber 类型的对象, 使用 forin 遍历输出
NSArray *array2 = [NSArray arrayWithObjects:@10, @12.34, @YES, nil];
for (NSNumber *num in array2) {
NSLog(@"%@", num);
}
NSLog(@"%@", array2);
2. NSMutableArray
创建可变数组对象
NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"aaa", @"lufei", @"soulong", @"wusuopu", @"namei", nil];
NSLog(@"%@", mArray);
数组元素的个数
NSUInteger count = [mArray count];
NSLog(@"%lu", count);
往数组中添加元素
//(1) 添加一个元素
[mArray addObject:@"zhangsan"];
NSLog(@"%@", mArray);
//(2) 插入一个元素
[mArray insertObject:@"lisi" atIndex:0];
NSLog(@"%@", mArray);
替换
[mArray replaceObjectAtIndex:0 withObject:@"wangwu"];
NSLog(@"%@", mArray);
交换下标对应的元素
[mArray exchangeObjectAtIndex:2 withObjectAtIndex:3];
NSLog(@"%@", mArray);
移除元素
//(1) 移除指定元素
[mArray removeObject:@"aaa"];
NSLog(@"%@", mArray);
//(2) 移除指定下标对应的元素
[mArray removeObjectAtIndex:0];
NSLog(@"%@", mArray);
//(3) 移出所有的元素
[mArray removeAllObjects];
NSLog(@"%@", mArray);
快速遍历
for (NSString *str in mArray) {
NSLog(@"%@", str);
}
二. 字典
//字典 NSDictionary : 用来存储具有一一对应关系的数据.
//一个 key 对应一个 value, key 起到唯一标识的作用, key 必须是唯一的.
//字典中存储的数据是无序的, 一个键值对构成字典的一个元素.
1. NSDictionary
创建字典对象
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"laowang", @"name", @"man", @"gender", @"18", @"age", nil];
NSLog(@"%@", dic);
字典元素个数
NSUInteger count = [dic count];
NSLog(@"%lu", count);
通过 Key 来获取 value
NSString *str = [dic objectForKey:@"name"];
NSLog(@"%@", str);
获取所有的 key
NSArray *array = [dic allKeys];
NSLog(@"%@", array);
字典遍历
//字典遍历获取到的是 key ,然后根据 key 可获得对应的 value 值.
for (NSString *string in dic) {
NSLog(@"%@", [dic valueForKey:string]);
}
2. NSMutableDictionary
创建可变字典对象
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"laowang", @"name", @"18", @"age", @"woman", @"gender", nil];
NSLog(@"%@", mDic);
向字典中添加元素
[mDic setObject:@"100" forKey:@"weight"];
NSLog(@"%@", mDic);
[mDic setValue:@"168" forKey:@"height"];
NSLog(@"%@", mDic);
修改 key 对应的 value
[mDic setValue:@"16816899168" forKey:@"height"];
NSLog(@"%@", mDic);
移除
[mDic removeObjectForKey:@"weight"];
NSLog(@"%@", mDic);
[mDic removeAllObjects];
NSLog(@"%@", mDic);
三. 集合
1. NSSet
创建集合对象
NSSet *set = [NSSet setWithObjects:@"AAA", @"aaa", @"bbb", @"aaa", @"aaa", nil];
NSLog(@"%@", set);
计算元素个数
NSLog(@"%lu", [set count]);
获取集合中的元素
NSString *str = [set anyObject];
NSLog(@"%@", str);
快速遍历
for (NSString *string in set) {
NSLog(@"%@", string);
}
2. NSMutableSet
创建可变集合对象
NSMutableSet *mSet = [NSMutableSet setWithObjects:@"QQ", @"pp", @"mm", @"sm", @"ms", @"cs", @"cs", nil];
NSLog(@"%@", mSet);
添加元素
[mSet addObject:@"hh"];
NSLog(@"%@", mSet);
移除元素
[mSet removeObject:@"hh"];
NSLog(@"%@", mSet);
NSCountedSet
NSCountedSet *countSet = [NSCountedSet setWithObjects:@"aaa", @"aaa", @"bb", @"bbb", @"aaa", @"bbb", nil];
NSLog(@"%@", countSet);
NSUInteger countAaa = [countSet countForObject:@"aaa"];
NSLog(@"%lu", countAaa);
总结
/**
* collection (数组, 字典, 集合)总结:
* 相同点: 都属于 collection(集合 --- 大容器), 都可以存储多个对象, 并且不限制对象的类型.
* 不同点:
* 1. 作用:
* 1) 数组: 用来管理有序集合.
* 2) 字典: 用于管理具有一一对应关系的数据的集合.
* 3) 集合: 用来管理具有无序性,并且具有互异特点的数据.
* 2. 特点:
* 1) 数组: 有序, 并且元素可重复.
* 2) 字典: 无序, 每个元素是一个键值对(一个 key 对应一个 value)切记: key 值唯一.
* 3) 集合: 无序, 互异
* 3. 取值方式:
* 1) 数组: 根据下标获取对应的元素
* 2) 字典: 根据 key 值获取对应的 value
* 3) 集合: anyObject --- 集合的效率最大化
* 4. 快速遍历:
* 1) 数组: 遍历到的是数组中的元素
* 2) 字典: 便利到的key 值, 根据 key 在字典中查找到 value 值
* 3) 集合: 遍历到的是集合中的元素
*/