数组常用方法

//创建集合
        //NSArray是不可变数组,一旦创建完成就不能对数组进行添加删除等操作
        NSArray * array = [[NSArray alloc] init];
        NSLog(@"%@",array);
        
        //通过构造方法创建一个NSArray,在创建一个NSSArray的时候,集合的最后一个元素一定是nil
        NSArray * array1 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d" ,nil];
        NSLog(@"%@",array1);
        
        //数组中可以存储不同类型的对象
        NSNumber * number = [NSNumber numberWithInt:1];
        NSArray * array2 = [NSArray arrayWithObjects:@"e",@"f",number,nil];
        NSLog(@"%@",array2);
        
        //数组实际存储的是对象的地址,所以也可以存储数组的地址
        NSArray * array3 = [NSArray arrayWithObjects:@"g",array1,array2,nil];
        NSLog(@"%@",array3);
        
        //存储自定义对象
        Person * person1 = [[Person alloc] initWithName:@"xiaoming" andAge:1];
        Person * person2 = [[Person alloc] initWithName:@"xiaoli" andAge:22];
        NSArray * array4 = [NSArray arrayWithObjects:person1,person2,nil];
        NSLog(@"%@",array4);
        
        //数组中存储基本类型,包装成NSNumber使用,把能将nil存储到NSArray中,会导致数据丢失
        NSString  * str = nil;
        NSArray * array5 = [[NSArray alloc] initWithObjects:@"a", str,[NSNumber numberWithInt:12],nil];
        NSLog(@"%@",array5);
        
        //创建数组的快捷方式
        NSArray * array6 = @[@"a",@"b",[NSNumber numberWithInt:1]];
        NSLog(@"%@",array6);
        
        //快速获得一个数组中的元素
        NSString * str2 = array6[0];
        NSLog(@"%@",str2);
        NSString * str3 = [array6 objectAtIndex:0];
        NSLog(@"%@",str3);
        
        //获取数组元素个数
        NSUInteger count = [array6 count];
        NSLog(@"%lu",count);
        
        //判断一个元素是否存在某个对象
        BOOL isContain = [array6 containsObject:@"b"];
        if (isContain) {
            NSLog(@"存在");
        }else{
            NSLog(@"不存在");
        }
        
        
        /*
         数组循环
         */
        //放法1   普通for 循环
        NSArray * array7 = @[@"a",@"b",@"c"];
        for (int i = 0; i<[array7 count]; i++) {
            NSLog(@"array7[%d] = %@",i,array7[i]);
        }
        //方法2   增强的for循环
        for (NSString * str in array7) {
            NSLog(@"array7中有 %@",str);
        }
        //方法3   枚举类型的循环
        NSEnumerator * enumerator = [array7 objectEnumerator];
        NSString * strEnum ;
        while (strEnum = [enumerator nextObject]) {
            NSLog(@"array7中有%@",strEnum);
        }
        
        /*
         数组排序
         */
        //1.使用sortedArrayUsingSelector:@selector(compare:)
        NSArray * sortArray1 = @[@"b",@"e",@"d",@"c",@"a"];
        NSLog(@"sortArray1排序前:%@",sortArray1);
        sortArray1 = [sortArray1 sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"sortArray1排序后:%@",sortArray1);
        
        //2.使用block方式a排序
        NSArray * sortArray2 = @[@"b",@"e",@"d",@"c",@"a"];
        NSLog(@"sortArray2排序前:%@",sortArray2);

        sortArray2 = [sortArray2 sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            return [obj1 compare:obj2];
        }];
        NSLog(@"sortArray2排序后:%@",sortArray2);
        
        //3.使用sortedArrayUsingDescriptors给自定义对象排序
        Person * sortPerson1 = [[Person alloc]initWithName:@"zhangsan" andAge:15];
        Person * sortPerson2 = [[Person alloc]initWithName:@"lisi" andAge:23];
        Person * sortPerson3 = [[Person alloc]initWithName:@"lisi" andAge:18];
        NSArray * personArray = @[sortPerson1,sortPerson2,sortPerson3];
        NSLog(@"personArray排序前:%@",personArray);
        //sortDescriptorWithKey 要排序的参数;ascending排序方式,YES正序,NO倒序
        NSSortDescriptor  * description1 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
        NSSortDescriptor * description2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
        NSArray * descriptorArray = @[description1,description2];
        personArray =  [personArray sortedArrayUsingDescriptors:descriptorArray];
        NSLog(@"personArray排序后:%@",personArray);

        //4.使用sortedArrayUsingComparator给自定义对象排序
        NSArray * personArray2 = @[sortPerson1,sortPerson2,sortPerson3];
        NSLog(@"personArray2排序前:%@",personArray2);
        personArray2 = [personArray2 sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            Person *p1 = obj1;
            Person * p2 = obj2;
            return [p1.name compare:p2.name];
        }];
        NSLog(@"personArray2排序后:%@",personArray2);


        /*
         可变数组
         */
        NSMutableArray * mutableArray = [[NSMutableArray alloc]init];
        [mutableArray addObject:@"one"];
        [mutableArray addObject:@"two"];
        [mutableArray addObject:@"three"];
        NSLog(@"mutableArray为:%@",mutableArray);
        
        //将元素插入到指定位置
        [mutableArray insertObject:@"four" atIndex:3];
        NSLog(@"mutableArray为:%@",mutableArray);
        
        //删除指定位置的对象
        [mutableArray removeObjectAtIndex:1];
        NSLog(@"mutableArray为:%@",mutableArray);
        
        //删除指定元素
        [mutableArray removeObject:@"three"];
        NSLog(@"mutableArray为:%@",mutableArray);
        
        //删除数组中所有的元素
        [mutableArray removeAllObjects];
        NSLog(@"mutableArray为:%@",mutableArray);
        
        [mutableArray addObject:@"one"];
        [mutableArray addObject:@"two"];
        [mutableArray addObject:@"three"];
        //遍历数组
        for(int i = 0 ;i < mutableArray.count; i++){
            NSString * str = [mutableArray objectAtIndex:i];
            NSLog(@"mutableArray[%d] = %@",i,str);
        }
        for (int i = 0; i < [mutableArray count]; i++) {
            NSLog(@"mutableArray[%d] = %@",i,mutableArray[i]);
        }
        for (NSString *str in mutableArray) {
            NSLog(@"mutableArray: %@",str);
        }
        NSEnumerator * mutableEnumerator = [mutableArray objectEnumerator];
        NSString * value;
        while (value = [mutableEnumerator nextObject]) {
            NSLog(@"mutableArray: %@",value);
        }

你可能感兴趣的:(数组常用方法)