第五讲.字典,集合,数组排序(快速排序,冒泡,默认排序)(源代码)

  1 #import <Foundation/Foundation.h>
  2 
  3 int main(int argc, const char * 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); 102 103 104 105 /* 106 可变集合(NSMutableSet) 107 108 */ 109 110 //定义可变集合 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); 119 120 //删除元素 121 [set3 removeObject:@"four"]; 122 NSLog(@"%@",set3); 123 124 //打印出集合中的所有元素 125 NSLog(@"%@",[set3 allObjects]); 126 127 //求两个集合的并集 128 [set3 unionSet:set2]; 129 NSLog(@"%@",set3); 130 131 //求两个集合的交集 132 [set3 minusSet:set2]; 133 NSLog(@"%@",set3); 134 135 //可变数组中国重新定义这个集合(相当于替换) 136 [set2 setSet:[NSSet setWithObjects:@"1",@"2",@"3", nil]]; 137 NSLog(@"%@",set2); 138 139 140 141 142 143 /* 144 145 -------------------------------------- 146 快速遍历的方法总结 147 148 -------------------------------------- 149 150 */ 151 //数组的快速遍历(得到的是数组中的元素对象) 152 NSArray *a = [NSArray arrayWithObjects:@"12",@"34",@"56", nil]; 153 for(id a1 in a) 154 { 155 NSLog(@"%@",a1); 156 } 157 158 //字典的快速遍历(遍历得到的是字典中的key值) 159 NSDictionary *b = [NSDictionary
dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil]; 160 for(id b1 in b) 161 { 162 NSLog(@"%@",b1); 163 164 } 165 166 //集合的快速遍历(得到的是集合的元素对象) 167 NSSet *c = [NSSet setWithObjects:@"one"@"four"@"two", nil]; 168 for(id c1 in c) 169 { 170 NSLog(@"%@",c1); 171 172 } 173 174 /* 175 176 --------------------------------------------- 177 178 oc中的数组排序(类似于冒泡排序) 179 180 --------------------------------------------- 181 182 */ 183 //oc中的冒泡排序 184 NSMutableArray *a2 = [NSMutableArray arrayWithObjects:@"12",@"34",@"56", nil]; 185 for (int i=0; i<[a2 count]-1; i++) { 186 for (int j=0; j<[a2 count]-1-i; j++) { 187 if ([[a2 objectAtIndex:j] compare:[a2 objectAtIndex:j+1]]) { 188 [a2 exchangeObjectAtIndex:j withObjectAtIndex:j+1]; 189 } 190 } 191 } 192 NSLog(@"%@",a2); 193 194 195 196 //数组中转化为整型然后比较 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 //注意要他们相应的转化为整型值,然后比较 209 for (int i=0; i<[array count]; i++) { 210 for (int j=0; j<[array count]-1-i; j++) { 211 if ([[array objectAtIndex:j] intValue]<[[array objectAtIndex:j+1]
integerValue]) {
212 [array exchangeObjectAtIndex:j withObjectAtIndex:j+1]; 213 } 214 } 215 } 216 NSLog(@"%@",array); 217 218 219 /* 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); 228 229 [arr sortUsingSelector:@selector(compare:)]; 230 NSLog(@"%@",arr); 231

 

原文地址:https://www.cnblogs.com/erdeng/p/4768105.html