第五讲:字典、集、数组排序(详情请参考源程序lession5-homework)
知识点:
字典:
1.字典用于保存具有映射关系数据的集合,一个key-value认为是一个条目(Entry),字典是存储key-value对的容器.
2.字典类的特点:字典靠key存取元素,key不能重复,value必须是对象,键值对在字典中是无序存储的.
3.不可变字典:字典一旦创建,键值对就不可更改,不可添加,不可删除,仅能读取key或value.可变字典可以对管理的键值对惊醒增、删、改.
集合(set):
1.集合的特点:集合中的元素唯一,存储的元素是无序的,存储元素必须是对象类型.IOS中用Set表示集合
2.不可变集合、可变集合和NSCountSet(能记录元素的重复次数,在set的基础上添加了计数功能->countForObject).
集合的快速枚举:(for...in...)
数组排序:(exchangeObjectAtIndex)
数组默认排序:不可变数组:[array sortedAarrayUsingSelector<#(SEL)#>]
可变数组:[mutableArray sortUsingSelector<#(SEL)#>]
@select,获取方法名.这个方法名师叔祖中的方法 //默认使用升序排列
//字典
//1.不可变字典
//方法1.初始化
//方法2
//键值对的数量
//获取所有的key值
//获取所有的value值
//获取某个key值的value;
//获取一个对象的所有key值
//可变字典
//方法1.初始化
//方法2.删除键值对
//删除某个键多对应的值
//删除所有的键值对
//方法3.增加键值对
//从一个字典中添加一个键值对
//方法4.设置键值对[Entry]的值
//集合
//不可变集合
//方法1.初始化
//2.集合中元素的个数
//3.随机取出集合中的元素
//4.判断集合中是否含有某个元素,若有则返回该值
//可变集合
//1.初始化(参考不可变集合初始化方法,此处只列举一个).
//2.往集合中添加元素
//3.删除某个元素
//4.删除全部元素.
//5.元素个数.
//NSCountedSet:计重复元素个数的功能
//初始化
//元素个数
//计重复元素个数
//快速枚举
//数组排序
//字典
/*
知识点:
字典:
1.字典用于保存具有映射关系数据的集合,一个key-value认为是一个条目(Entry),字典是存储key-value对的容器.
2.字典类的特点:字典靠key存取元素,key不能重复,value必须是对象,键值对在字典中是无序存储的.
3.不可变字典:字典一旦创建,键值对就不可更改,不可添加,不可删除,仅能读取key或value.可变字典可以对管理的键值对惊醒增、删、改.
集合(set):
1.集合的特点:集合中的元素唯一,存储的元素是无序的,存储元素必须是对象类型.IOS中用Set表示集合
2.不可变集合、可变集合和NSCountSet(能记录元素的重复次数,在set的基础上添加了计数功能->countForObject).
集合的快速枚举:(for...in...)
数组排序:(exchangeObjectAtIndex)
数组默认排序:不可变数组:[array sortedAarrayUsingSelector<#(SEL)#>]
可变数组:[mutableArray sortUsingSelector<#(SEL)#>]
@select,获取方法名.这个方法名师叔祖中的方法 //默认使用升序排列
//字典
//1.不可变字典
//方法1.初始化
//方法2
//键值对的数量
//获取所有的key值
//获取所有的value值
//获取某个key值的value;
//获取一个对象的所有key值
//可变字典
//方法1.初始化
//方法2.删除键值对
//删除某个键多对应的值
//删除所有的键值对
//方法3.增加键值对
//从一个字典中添加一个键值对
//方法4.设置键值对[Entry]的值
//集合
//不可变集合
//方法1.初始化
//2.集合中元素的个数
//3.随机取出集合中的元素
//4.判断集合中是否含有某个元素,若有则返回该值
//可变集合
//1.初始化(参考不可变集合初始化方法,此处只列举一个).
//2.往集合中添加元素
//3.删除某个元素
//4.删除全部元素.
//5.元素个数.
//NSCountedSet:计重复元素个数的功能
//初始化
//元素个数
//计重复元素个数
//快速枚举
//数组排序
//字典
/*
//字典
/*
//1.不可变字典
//方法1.初始化
//初始化方法1.
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"yangxiutao" forKey:@"name"];
NSLog(@"%@",dic);
//初始化方法2.
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"yangxiutao",@"name",@"23",@"age",@"man",@"sex", nil];
NSLog(@"%@",dic1);
//初始化方法3.
NSArray *array = [NSArray arrayWithObjects:@"yangxiutao",@"man",@"24", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"name",@"sex",@"age", nil];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjects:array forKeys:array2];
NSLog(@"%@",dic2);
//初始化方法4.
NSDictionary *dic3 = [NSDictionary dictionary];
dic3 = dic;
NSLog(@"%@",dic3);
//方法2
NSArray *array = [NSArray arrayWithObjects:@"yangxiutao",@"man",@"24",@"jiangkun",@"man",@"23", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"name",@"sex",@"age",@"name1",@"sex1",@"age1", nil];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjects:array forKeys:array2];
NSLog(@"%@",dic2);
//键值对的数量
NSInteger num = [dic2 count];
NSLog(@"%lu",num);
//获取所有的key值
NSArray *array1 = [dic2 allKeys];
NSLog(@"%@",array1);
//获取所有的value值
NSArray *array3 = [dic2 allValues];
NSLog(@"%@",array3);
//获取某个key值的value;
NSString *str = [dic2 objectForKey:@"name"];
NSLog(@"%@",str);
//获取一个对象的所有key值
NSArray *array4 = [dic2 allKeysForObject:@"yangxiutao"];
NSLog(@"%@",array4);
//可变字典
//方法1.初始化
//初始化方法1.
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:@"yangxiutao" forKey:@"name"];
NSLog(@"%@",dic);
//初始化方法2.
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"yangixutao",@"name",@"man",@"sex",@"23",@"age", nil];
NSLog(@"%@",dic1);
//初始化方法3.
// NSMutableArray *array = [NSMutableArray arrayWithObjects:@"yangixutao",@"man",@"23", nil];
// NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"name",@"sex",@"age", nil];
NSArray *array = [NSArray arrayWithObjects:@"yangixutao",@"man",@"23", nil];
NSArray *array1 = [NSArray arrayWithObjects:@"name",@"sex",@"age", nil];
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjects:array forKeys:array1];
NSLog(@"%@",dic2);
//初始化方法4.
NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithDictionary:dic2];
NSLog(@"%@",dic3);
//方法2.删除键值对
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"yangixutao",@"name",@"man",@"sex",@"23",@"age",nil];
//删除某个键多对应的值
[dic1 removeObjectForKey:@"sex"];
NSLog(@"%@",dic1);
//删除所有的键值对
[dic1 removeAllObjects];
NSLog(@"%@",dic1);
//方法3.增加键值对
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"yangixutao",@"name",@"man",@"sex",@"23",@"age", nil];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"li",@"name1",@"18",@"age1",@"nv",@"sex1", nil];
//从一个字典中添加一个键值对
[dic1 addEntriesFromDictionary:dic];
NSLog(@"%@",dic1);
//方法4.设置键值对[Entry]的值
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"yangixutao",@"name",@"man",@"sex",@"23",@"age", nil];
//根据某个键值设置Value的值,用setObject:(id) forKey:(id<NSCopying>)设置Value值时,当Value为nil时,程序直接崩溃.
[dic1 setObject:@"li" forKey:@"name"];
NSLog(@"%@",dic1);
// [dic1 setObject:nil forKey:@"name"];
// NSLog(@"%@",dic1);
//根据某个键值设置Value的值,用setValue:(id) forKey:(NSString *)设置Value值时,当Value为nil时,程序直接删除该条目.
[dic1 setValue:@"y" forKey:@"name"];
NSLog(@"%@",dic1);
[dic1 setValue:nil forKey:@"name"];
NSLog(@"%@",dic1);
*/
//集合
/*
//不可变集合
//方法1.初始化
//初始化只有一个元素的集合
NSSet *set = [NSSet setWithObject:@"fdg"];
NSLog(@"%@",set);
//初始化一个含有多个元素的集合
NSSet *set1 = [NSSet setWithObjects:@"qqwxv",@"frscg",@"hgrt",@"boofl", nil];
NSLog(@"%@",set1);
//用集合初始化集合
NSSet *set2 = [NSSet setWithSet:set];
NSLog(@"%@",set2);
//用数组初始化集合
NSArray *array = [NSArray arrayWithObjects:@"sdf",@"23",@"ht",@"fgf", nil];
NSSet *set3 = [NSSet setWithArray:array];
NSLog(@"%@",set3);
//2.集合中元素的个数
NSSet *set1 = [NSSet setWithObjects:@"qqwxv",@"frscg",@"hgrt",@"boofl", nil];
[set1 count];
NSLog(@"%lu",(unsigned long)set1.count);
//3.随机取出集合中的元素
NSSet *set1 = [NSSet setWithObjects:@"qqwxv",@"frscg",@"hgrt",@"boofl", nil];
[set1 anyObject];
NSLog(@"%@",set1.anyObject);
//4.判断集合中是否含有某个元素,若有则返回该值
NSSet *set1 = [NSSet setWithObjects:@"qqwxv",@"frscg",@"hgrt",@"boofl", nil];
NSString *str = [set1 member:@"boofl"];
NSLog(@"%@",str);
//5.判断集合中是否含有某个元素,若有则返回TRUE
NSSet *set1 = [NSSet setWithObjects:@"qqwxv",@"frscg",@"hgrt",@"boofl", nil];
[set1 containsObject:@"23fsdf"];
NSLog(@"%i",[set1 containsObject:@"fgdf"]);
NSLog(@"%i",[set1 containsObject:@"qqwxv"]);
//可变集合
//1.初始化(参考不可变集合初始化方法,此处只列举一个).
NSMutableSet *set = [NSMutableSet setWithObjects:@"re",@"ff",@"ger",@"hys", nil];
//2.往集合中添加元素
[set addObject:@"yangxiutao"];
NSLog(@"%@",set);
//3.删除某个元素
[set removeObject:@"re"];
NSLog(@"%@",set);
//4.删除全部元素.
// [set removeAllObjects];
// NSLog(@"%@",set);
//5.元素个数.
NSLog(@"%li",set.count);
//NSCountedSet:计重复元素个数的功能
//初始化
NSCountedSet *set1 = [NSCountedSet setWithObjects:@"111",@"222",@"333",@"444",@"444",nil];
NSLog(@"%@",set1);
//元素个数
[set1 count];
NSLog(@"%lu",set1.count);
//计重复元素个数
[set1 countForObject:@"111"];
NSLog(@"%lu",[set1 countForObject:@"444"]);
*/
//快速枚举
/*
NSSet *set1 = [NSSet setWithObjects:@"qqwxv",@"frscg",@"hgrt",@"boofl", nil];
for (NSString *str in set1) {
NSLog(@"%@",str);
}
*/
//数组排序
/*
//typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"fsd",@"te",@"wf",@"rh", nil];
for (int i = 0; i < array.count - 1; i++) {
for (int j = 0 ; j < array.count - 1 - i; j++) {
if ([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1]] == 1) {
[array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
NSLog(@"%@",array);
*/