一、集合的概念
集合中的元素是无序不重复的。
集合和数组类似,区别在于:数组的元素是有序的,集合的元素是无序的。
集合的种类:
NSSet
-NSMutableSet
NSIndexSet
-NSMutableIndexSet
二、NSSet的使用
1.创建集合
NSSet * set = [[NSSet alloc] initWithObjects: @"one", @"two", @"three", @"four", nil]; NSLog(@"%@", set);
还可以使用数组来创建集合
NSArray * array = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", @"four", nil]; NSSet * set2 = [[NSSet alloc] initWithArray:array];
NSSet * set = [[NSSet alloc] initWithObjects: @"one", @"two", @"three", @"four", @"three", nil]; NSLog(@"%@", set);
2015-09-26 01:09:23.791 NSSet[4663:97717] {( one, three, two, four )}3.集合的大小
NSSet * set = [[NSSet alloc] initWithObjects: @"one", @"two", @"three", @"four", @"three", nil]; NSInteger nsi = [set count]; NSLog(@"%lu", nsi);
BOOL ret = [set containsObject: @"one"]; if(ret){ NSLog(@"包含"); }else{ NSLog(@"不包含"); }
5.判断两个集合是否相等
//判断下面两个集合是否相等,结果是相等的 NSSet * set = [[NSSet alloc] initWithObjects: @"one", @"two", @"three", @"four", @"three", nil]; NSSet * set2 = [[NSSet alloc] initWithObjects:@"one", @"two", @"three", @"four", nil]; BOOL ret2 = [set isEqual:set2]; if(ret2){ NSLog(@"相等"); }else{ NSLog(@"不相等"); }
如果一个集合A包含了另一个集合B的所有元素,那个集合B是集合A的子集
BOOL ret3 = [set isSubsetOfSet: set2]; if(ret3){ NSLog(@"set是set2的子集,因为set2包含set的所有元素");//即使顺序不相同也可以 }else{ NSLog(@"不是"); }
NSSet * set = [[NSSet alloc] initWithObjects: @"one", @"two", @"three", @"four", nil]; NSEnumerator * enumerator = [set objectEnumerator]; id obj; while(obj = [enumerator nextObject]){ NSLog(@"%@", obj); }<pre name="code" class="objc">for(id obj in set){ NSLog(@"%@", obj); }
8.将集合的元素取出,生成数组
NSSet * set = [[NSSet alloc] initWithObjects: @"one", @"two", @"three", @"four", nil]; NSArray * array = [set allObjects]; NSLog(@"%@", array);
三、NSMutableSet可变集合
1.创建对象
NSMutableSet * mutableSet = [[NSMutableSet alloc] initWithObjects: @"one", @"two", @"three", nil]; NSLog(@"%@", mutableSet); //如果添加的元素有重复的话,只保留其中一个 NSMutableSet * mutableSet2 = [[NSMutableSet alloc] init]; [mutableSet2 addObject:@"one"]; [mutableSet2 addObject:@"two"]; [mutableSet2 addObject:@"three"]; [mutableSet2 addObject:@"two"]; NSLog(@"%@", mutableSet2);
NSMutableSet * mutableSet = [[NSMutableSet alloc] initWithObjects: @"one", @"two", @"three", nil]; NSLog(@"原集合:%@", mutableSet); //增加元素 [mutableSet addObject:@"four"]; NSLog(@"增加元素:%@", mutableSet); //删除元素 [mutableSet removeObject:@"one"]; //[mutableSet removeAllObjects]; NSLog(@"删除元素:%@", mutableSet); //添加集合 NSSet * set = [[NSSet alloc] initWithObjects: @"four", @"five", @"six", nil]; [mutableSet unionSet: set]; NSLog(@"增加集合:%@", mutableSet); //删除集合 [mutableSet minusSet:set]; NSLog(@"删除集合:%@", mutableSet);输出结果:
2015-09-26 01:38:21.803 NSSet[5237:109282] 原集合:{( one, two, three )} 2015-09-26 01:38:21.805 NSSet[5237:109282] 增加元素:{( one, three, two, four )} 2015-09-26 01:38:21.805 NSSet[5237:109282] 删除元素:{( three, two, four )} 2015-09-26 01:38:21.806 NSSet[5237:109282] 增加集合:{( five, three, two, four, six )} 2015-09-26 01:38:21.806 NSSet[5237:109282] 删除集合:{( three, two )} Program ended with exit code: 0
该集合类型存储的都是一堆索引,所以该集合叫做索引集合或指数集合
//NSIndexSet索引集合、指数集合 NSIndexSet * indexSet = [[NSIndexSet alloc] initWithIndexesInRange: NSMakeRange(1, 3)]; NSLog(@"%@", indexSet);//1,2,3 NSArray * array = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", @"four", nil]; id obj = [array objectAtIndex: 1]; NSLog(@"取出数组中的某个元素:%@", obj); NSArray * array2 = [array objectsAtIndexes: indexSet]; NSLog(@"根据一堆索引,找出数组中的一堆元素,组成新的数组:%@", array2);
可变的索引集合
NSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init]; [indexSet addIndex: 0]; [indexSet addIndex: 2]; [indexSet addIndex: 3]; NSArray * array = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", @"four", nil]; id obj = [array objectAtIndex: 1]; NSLog(@"取出数组中的某个元素:%@", obj); NSArray * array2 = [array objectsAtIndexes: indexSet]; NSLog(@"根据一堆索引,找出数组中的一堆元素,组成新的数组:%@", array2);
@诗未冷学习博客