第五讲.字典,集合,数组排序(快速排序,冒泡,默认排序)(源代码)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了第五讲.字典,集合,数组排序(快速排序,冒泡,默认排序)(源代码),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5375字,纯文字阅读大概需要8分钟。
内容图文
1
#import <Foundation/Foundation.h>
2 3int main(int argc, constchar * argv[]) {
4 @autoreleasepool {
5 6//字典的使用
7 8//创建字典对象 9 NSDictionary * a1 = [NSDictionary
dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];
10 NSLog(@"%@",a1);
11 12 13 NSDictionary *a3 = [[NSDictionary alloc]
initWithObjectsAndKeys:@"hu",@"name",@"23",@"age", nil];
14 NSDictionary *a4 = [NSDictionary dictionaryWithDictionary:a1];
15 NSLog(@"%@ %@ %@",a1,a3,a4);
16 17 18//获取所有key值,获取所有value值 19 NSLog(@"%@ %@",[a1 allKeys],[a1 allValues]);
20 21 22//通过key值查询value 23 24 NSLog(@"%@",[a1 objectForKey:@"2"]);
25 26 27//获取值的个数 28 NSLog(@"%lu",[a1 count]);
29 30 31 32 33 34//不可变字典 NSMutableDictionary
35 36 37//定义不可变字典 38 39 NSMutableDictionary *dic = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"xiaoge",@"name",@"18",@"age",@"nan",@"sex", nil];
40 NSLog(@"%@",dic);
41 42 NSMutableDictionary *dic1 = [NSMutableDictionary
dictionaryWithObjects:[NSArray arrayWithObjects:@"cheng",@"deng",@"hui", nil]
forKeys:[NSArray arrayWithObjects:@"1",@"2",@"3", nil]];
43 NSLog(@"%@",dic1);
44 NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithDictionary:dic];
45 NSLog(@"%@",dic2);
46 47 48//增添键值对 49 50 [dic setObject:@"five" forKey:@"5"];
51 NSLog(@"%@",dic);
52 53 54//删除键值对 55 56 [dic removeObjectForKey:@"5"];
57 NSLog(@"%@",dic);
58 59//更改键值对 60 NSMutableDictionary *a5 = [[NSMutableDictionary alloc]
initWithObjects:[NSArray arrayWithObjects:@"one",@"two",@"three", nil]
forKeys:[NSArray arrayWithObjects:@"1",@"2",@"3", nil]];
61 NSLog(@"%@",a5);
62 63 [a5 setObject:@"six" forKey:@"1"];
64 NSLog(@"%@",a5);
65 66/* 67 ----------------------------------------------
68 69 集合(NSSet)
70 ----------------------------------------------
71 72*/ 73 74 75//定义集合 76 NSSet *set = [[NSSet alloc] initWithObjects:@"one",@"two",@"three", nil];
77 78 NSLog(@"%@",set);
79 80//判断是否为集合中的元素 81 NSLog(@"%@",[set member:@"one"]);
82 83//打印出来总打印顺序的第一个 84 NSLog(@"%@",[set anyObject]);
85 86 87//打印出来所有元素 88 NSLog(@"%@",[set allObjects]);
89 90//打印出元素个数 91 NSLog(@"%lu",(unsigned long)[set count]);
92 93 94//判断集合是否包含这个元素(返回值为1或0) 95 BOOL ret = [set containsObject:@"one"];
96 NSLog(@"%hhd",ret);
97 98//判断两个集合是否相等 99 NSSet *set1 = [NSSet setWithObjects:@"one",@"two",@"three", nil];
100 BOOL r = [set isEqualTo:set1];
101 NSLog(@"%hhd",r);
102103104105/*106 可变集合(NSMutableSet)
107108*/109110//定义可变集合
111//利用一个集合创建另一个集合112 NSMutableSet * set2 = [NSMutableSet setWithSet:set];
113 NSLog(@"%@",set2);
114 NSMutableSet *set3 = [NSMutableSet setWithObjects:@"one",@"two",@"three", nil];
115//添加数组元素(重复的只保留一次)116 [set3 addObject:@"four"];
117 [set3 addObject:@"five"];
118 NSLog(@"%@",set3);
119120//删除元素121 [set3 removeObject:@"four"];
122 NSLog(@"%@",set3);
123124//打印出集合中的所有元素125 NSLog(@"%@",[set3 allObjects]);
126127//求两个集合的并集128 [set3 unionSet:set2];
129 NSLog(@"%@",set3);
130131//求两个集合的交集132 [set3 minusSet:set2];
133 NSLog(@"%@",set3);
134135//可变数组中国重新定义这个集合(相当于替换)136 [set2 setSet:[NSSet setWithObjects:@"1",@"2",@"3", nil]];
137 NSLog(@"%@",set2);
138139140141142143/*144145 --------------------------------------
146 快速遍历的方法总结
147148 --------------------------------------
149150*/151//数组的快速遍历(得到的是数组中的元素对象)152 NSArray *a = [NSArray arrayWithObjects:@"12",@"34",@"56", nil];
153for(id a1 in a)
154 {
155 NSLog(@"%@",a1);
156 }
157158//字典的快速遍历(遍历得到的是字典中的key值)159 NSDictionary *b = [NSDictionary
dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];
160for(id b1 in b)
161 {
162 NSLog(@"%@",b1);
163164 }
165166//集合的快速遍历(得到的是集合的元素对象)167 NSSet *c = [NSSet setWithObjects:@"one"@"four"@"two", nil];
168for(id c1 in c)
169 {
170 NSLog(@"%@",c1);
171172 }
173174/*175176 ---------------------------------------------
177178 oc中的数组排序(类似于冒泡排序)
179180 ---------------------------------------------
181182*/183//oc中的冒泡排序184 NSMutableArray *a2 = [NSMutableArray arrayWithObjects:@"12",@"34",@"56", nil];
185for (int i=0; i<[a2 count]-1; i++) {
186for (int j=0; j<[a2 count]-1-i; j++) {
187if ([[a2 objectAtIndex:j] compare:[a2 objectAtIndex:j+1]]) {
188 [a2 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
189 }
190 }
191 }
192 NSLog(@"%@",a2);
193194195196//数组中转化为整型然后比较197 NSMutableArray * array = [NSMutableArray arrayWithObjects:
198 [NSNumber numberWithInt:12],
199 [NSNumber numberWithInt:70],
200 [NSNumber numberWithInt:34],
201 [NSNumber numberWithInt:37],
202 [NSNumber numberWithInt:50],
203 [NSNumber numberWithInt:90],
204 [NSNumber numberWithInt:21],
205 [NSNumber numberWithInt:3],
206 [NSNumber numberWithInt:68],
207 [NSNumber numberWithInt:1], nil];
208//注意要他们相应的转化为整型值,然后比较209for (int i=0; i<[array count]; i++) {
210for (int j=0; j<[array count]-1-i; j++) {
211if ([[array objectAtIndex:j] intValue]<[[array objectAtIndex:j+1]
integerValue]) {
212 [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
213 }
214 }
215 }
216 NSLog(@"%@",array);
217218219/*220 ----------------------------------
221 数组默认排序的使用
222 @selector 获取方法名,这个方法名是数组元素的方法
223 -----------------------------------
224*/225 NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"ae",@"be",@"ce", nil];
226 NSArray *arr1 = [arr sortedArrayUsingSelector:@selector(compare:)];
227 NSLog(@"%@",arr1);
228229 [arr sortUsingSelector:@selector(compare:)];
230 NSLog(@"%@",arr);
231
原文:http://www.cnblogs.com/erdeng/p/4768105.html
内容总结
以上是互联网集市为您收集整理的第五讲.字典,集合,数组排序(快速排序,冒泡,默认排序)(源代码)全部内容,希望文章能够帮你解决第五讲.字典,集合,数组排序(快速排序,冒泡,默认排序)(源代码)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。