zoukankan      html  css  js  c++  java
  • 常用基础OC 集合

     //    2016071917:50:53    集合

            

            

            //七、NSSet 集合对象(容器类,)

            

            

            //  1. 使用类方法创建对象

            

            NSSet *set1 = [NSSet set];  //  创建一个空的集合对象

            

            NSSet *set2 = [NSSet setWithObject:@"abc"];

            

            NSSet *set3 = [NSSet setWithObjects:@"abc", @"aaa", @"bbb", nil];

            

            NSLog(@"%@", set3);

            

            

            

            NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c", nil];

            

            NSSet *set4 = [NSSet setWithArray:array];   //  使用数组创建

            

            NSLog(@"%@", set4);

            

            

            

            NSSet *set5 = [NSSet setWithSet:set4];      //  使用集合创建

            

            NSLog(@"%@", set5);

            

            

            

            //  2.使用实例方法创建

            

            NSSet *set6 = [[NSSet alloc] init];

            

            NSLog(@"%@", set6);

            

            NSSet *set7 = [[NSSet alloc] initWithObjects:@"hello", @"hhaa", @"bbjdh", nil];

            

            NSLog(@"%@", set7);

            

            NSSet *set8 = [[NSSet alloc] initWithArray:array];

            

            NSLog(@"%@", set8);

            

            NSSet *set9 = [[NSSet alloc] initWithSet:set7];

            

            NSLog(@"%@", set9);

            

            

            

            //  3.返回几个元素个数

            

            NSLog(@"%ld", [set7 count]);

            

            

            

            //  4.枚举器访问集合元素

            

            NSEnumerator *enumerator = [set7 objectEnumerator];

            

            NSString *str = nil;

            

            while (str = [enumerator nextObject]) {

                

                NSLog(@"%@", str);

                

            }

            

            

            

            //  5.判断两个几个是否有交集

            

            BOOL ifhasIntersection = [set2 intersectsSet:set3];

            

            NSLog(@"%d", ifhasIntersection);

            

            

            

            //  6.判断两个集合是否相等

            

            NSLog(@"%d", [set2 isEqualToSet:set3]);

            

            

            

            //  7.判断当前集合是否是子集

            

            NSLog(@"%d", [set2 isSubsetOfSet:set3]);

            

            

            

            //5.2可变集合 NSMutableSet

            

            //  创建指定元素个数的一个集合对象

            

            NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:4];

            

            [mutableSet addObject:@"aaa"];

            

            NSLog(@"%@", mutableSet);

            

            //  类方法创建可变集合

            

            NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"aaa", @"bbb", @"ccc", nil];

            

            NSMutableSet *mutableSet2 = [NSMutableSet setWithObject:@"aaa"];

            

            

            

            //  添加一个对象到集合

            

            [mutableSet2 addObject:@"ddd"];

            

            NSLog(@"%@", mutableSet2);

            

            

            

            //  从集合中删除一个对象

            

            [mutableSet2 removeObject:@"ddd"];

            

            NSLog(@"%@", mutableSet2);

            

            

            

            //  把数组对象添加到集合对象中

            

            NSArray *arr10 = [NSArray arrayWithObjects:@"eee", @"fff", nil];

            

            [mutableSet1 addObjectsFromArray:arr10];

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  得到两个集合的交集 注意intersectSetintersectsSet的区别,后者是判断是否有交集的方法, 返回的是bool

            

            [mutableSet1 intersectSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  从一个集合中减去另一个集合

            

            [mutableSet1 minusSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  从一个元素中删除所有元素

            

            [mutableSet2 removeAllObjects];

            

            NSLog(@"%@", mutableSet2);

            

            

            

            //  取两个集合的并集

            

            [mutableSet1 unionSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  设置给集合赋值

            

            [mutableSet1 setSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

  • 相关阅读:
    最短路径:HDU2006-一个人的旅行(多个起点,多个终点)
    最短路径(最基础,经典的模板和思想):HDU-2544最短路
    数学算法:poweroj1026-丑数(根据固定倍数得到从小到大的序列)
    动态规划:ZOJ1074-最大和子矩阵 DP(最长子序列的升级版)
    数论:HDU1066-Last non-zero Digit in N!
    容斥原理:HDU-4135Co-prime
    数学算法:求一个数的质因子
    动态规划(入门,滚动数组,记录的都是状态):SWUSTACM-1010 魔兽争霸之最后的反击
    动态规划(入门):各种数字三角形
    动态规划:HDU2571-命运
  • 原文地址:https://www.cnblogs.com/DafaRan/p/5700727.html
Copyright © 2011-2022 走看看