05 OC之 集合 Set的使用
2016-07-20 12:25
447 查看
***********************
不可变集合
Immutable Set***********************
1、创建对象集合set
NSSet *set1 = [[NSSet
alloc]
initWithObjects:str1,str2, nil];
NSArray *array = [NSArray
arrayWithObjects:str1,str2,@"zzxczv",
nil];
2、将数组
放到集合
中
NSSet *set3 = [NSSet
setWithArray:array];
3、将集合转换为数组
NSArray *array1 = [set3
allObjects];
4、集合中元素个数
NSLog(@"集合中元素个数set3.count =
%ld",[set3 count]);
5、从集合中获取某元素,不能保证随机
NSString *str3 = [set3
anyObject];
6、集合中
没有
重复元素
NSSet *set4 = [NSSet
setWithObjects:str1,str1,str2,str2,
nil];
7、将一个集合
赋给
另一个集合
NSSet *set5 = [[NSSet
alloc]
initWithSet:set4];
8、两集合相加
NSSet *set6 = [set5
setByAddingObjectsFromSet:set3];
NSLog(@"set5 = %@",set5); //不变
************************* 可变集合Mutable Set *************************
1、可变集合的创建
NSMutableSet *mSet1 = [[NSMutableSet
alloc]
initWithCapacity:2];
NSString *str7 =
@"What do you want?";
2、将数组str7放入集合mSet1
[mSet1
addObject:str7];
3、类方法
NSMutableSet *mSet2 = [NSMutableSet
setWithObjects:str1,str2,mSet1,
nil];
4、移除一个元素 removeObject
[mSet2
removeObject:@"qwertyuiop"];
5、并集
[mSet2
unionSet:set1];
6、 取交集
//Removes from the receiving set each object that isn’t a member of another given set.
[mSet2
intersectSet:set1];
7、删除所有元素
[mSet2
removeAllObjects];
NSMutableSet *mSet3 = [[NSMutableSet
alloc]
initWithCapacity:3];
8、用 set 重置集合并将集合
set1 给 mSet3.
[mSet3
setSet:set1];
*[mSet3 setSet:mSet2];//在此之前 mSet2 已被清空。该行执行后,mSet3 = {()} 空集合
不可变集合
Immutable Set***********************
1、创建对象集合set
NSSet *set1 = [[NSSet
alloc]
initWithObjects:str1,str2, nil];
NSArray *array = [NSArray
arrayWithObjects:str1,str2,@"zzxczv",
nil];
2、将数组
放到集合
中
NSSet *set3 = [NSSet
setWithArray:array];
3、将集合转换为数组
NSArray *array1 = [set3
allObjects];
4、集合中元素个数
NSLog(@"集合中元素个数set3.count =
%ld",[set3 count]);
5、从集合中获取某元素,不能保证随机
NSString *str3 = [set3
anyObject];
6、集合中
没有
重复元素
NSSet *set4 = [NSSet
setWithObjects:str1,str1,str2,str2,
nil];
7、将一个集合
赋给
另一个集合
NSSet *set5 = [[NSSet
alloc]
initWithSet:set4];
8、两集合相加
NSSet *set6 = [set5
setByAddingObjectsFromSet:set3];
NSLog(@"set5 = %@",set5); //不变
************************* 可变集合Mutable Set *************************
1、可变集合的创建
NSMutableSet *mSet1 = [[NSMutableSet
alloc]
initWithCapacity:2];
NSString *str7 =
@"What do you want?";
2、将数组str7放入集合mSet1
[mSet1
addObject:str7];
3、类方法
NSMutableSet *mSet2 = [NSMutableSet
setWithObjects:str1,str2,mSet1,
nil];
4、移除一个元素 removeObject
[mSet2
removeObject:@"qwertyuiop"];
5、并集
[mSet2
unionSet:set1];
6、 取交集
//Removes from the receiving set each object that isn’t a member of another given set.
[mSet2
intersectSet:set1];
7、删除所有元素
[mSet2
removeAllObjects];
NSMutableSet *mSet3 = [[NSMutableSet
alloc]
initWithCapacity:3];
8、用 set 重置集合并将集合
set1 给 mSet3.
[mSet3
setSet:set1];
*[mSet3 setSet:mSet2];//在此之前 mSet2 已被清空。该行执行后,mSet3 = {()} 空集合
相关文章推荐
- Ruby中的集合编写指南
- set 命令特殊用法
- AVG 7.5.1.43 版本 序列号 集合
- SQL Server游标的使用/关闭/释放/优化小结
- 编写高质量代码改善C#程序――使用泛型集合代替非泛型集合(建议20)
- You must SET PASSWORD before executing this statement的解决方法
- 批处理 Set 命令详解 让你理解set命令第1/2页
- C#检查指定对象是否存在于ArrayList集合中的方法
- ASP中set与dim的区别(自己的理解)
- 深入C#中get与set的详解
- sql Set IDENTITY_INSERT的用法
- set_include_path在win和linux下的区别
- C#写入对象或集合类型数据到xml文件的方法
- C#中的集合用法分析
- C#集合遍历时删除和增加元素的方法
- C#常见的几种集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍历方法对比
- C#遍历集合与移除元素的方法
- JavaScript Archive Network 集合
- Backbone.js中的集合详解
- jquery中常用的SET和GET