OC基础-NSSet集合

一、集合的概念

集合中的元素是无序不重复的。

集合和数组类似,区别在于:数组的元素是有序的,集合的元素是无序的。

集合的种类:

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];

2.集合的元素是不重复的,重复的元素算一个

 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);

4.判断集合是否包含某个元素

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(@"不相等");
        }

6.判断一个集合是否是另一个集合的子集。

如果一个集合A包含了另一个集合B的所有元素,那个集合B是集合A的子集

  BOOL ret3 = [set isSubsetOfSet: set2];
        if(ret3){
            NSLog(@"set是set2的子集,因为set2包含set的所有元素");//即使顺序不相同也可以
        }else{
            NSLog(@"不是");
        }

7.遍历集合

  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);

2.增删元素

 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索引集合、指数集合
        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的使用

可变的索引集合

        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);

在上面的例子中,indexSet的元素如果超出了array的索引范围,会发生数组越界错误。


@诗未冷学习博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值