美文网首页
iOS 数组方法大全

iOS 数组方法大全

作者: sheldon_龙 | 来源:发表于2018-12-28 09:38 被阅读0次

    一.不可变数组

     //创建空数组
    NSArray *arr = [[NSArray alloc]init];
    //对象方法创建数组 以nil结束
    NSArray *arr1= [NSArray alloc]initWithObjects:@"sun",@"is",@"girl", nil];
    //用已有数组创建新数组
    NSArray *arr2 = [[NSArray array]initWithArray:arr];
    //通过类方法创建数组
    NSArray *arr3 = [NSArray arrayWithArray:arr2];
    //类方法创建空数组
    NSArray *arr4 = [NSArray array];
    

    1.不可变数组的方法

    //取一个元素
    NSArray *arr = @[@"sun",@"is",@"a",@"smilling",@"girl"];
    NSString *string = [arr objectAtIndex:2];
    //取第一个元素
    NSString *first = [arr firstObject];
    //取最后一个元素
    NSString *last = [arr lastObject];
    //简单方法取一个元素
    NSString *str2 = arr[3];
    
    //取多个
    //NSIndexSet:是一个索引集合,原理上与数组类似,只不过这是一个无序的
    NSIndexSet *set4 = [[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(1, 2)];
    NSArray *arr3 = [array1 objectAtIndex:set4];
    
    //简单方法截取多个元素
    NSArray *arr4 = [array1 subarrayWithRange:NSMakeRange(2, 3)];
    
    //for in 快速遍历  只针对数组来说,in的前面是数组中的元素, in后面是要遍历的集合(数组/字典)
    for (NSString *str in array1) {
      NSLog(@"%@",str);
    }
    

    二.可变数组

    1.创建

    //创建空数组
    NSMutableArray *mutArray = [NSMutableArray array];
    //capacity
    NSMutableArray *mutAr = [NSMutableArray arrayWithCapacity:100];
    NSMutableArray *mutcap = [[NSMutableArray alloc]initWithCapacity:100];
    NSArray *array = @[@"dskfj",@"df",@"sd",@"f"];
    NSMutableArray *mutarr3 = [[NSMutableArray alloc]initWithArray:array];
    //通过类方法
    NSMutableArray *mutarr5 = [NSMutableArray arrayWithObjects:@"sdj",@"dsf", nil];
    

    2.增删改查

    //增
    NSMutableArray *mutArray1 = [NSMutableArray arrayWithObjects:@"sunck", @"is", @"a", @"nice", @"man", nil];
    //增加到数组的结尾
    //1、增加一个元素
    [mutArray1 addObject:@"!"];
    NSLog(@"mutArray1-1 = %@", mutArray1);
    //2、增加多个
    [mutArray1 addObjectsFromArray:@[@"Oh", @"right"]];
    NSLog(@"mutArray1-2 = %@", mutArray1);
    //在数组中增加
    
    //注意:如果数组中有多个相同的元素,那么会全部删除
    //1、删除一个元素
    //1.1按元素删除  注意:如果数组中有多个相同的元素,那么会全部删除
     NSMutableArray *mutArray = [NSMutableArray arrayWithObjects:@"sd",@"sun",@"id",@"good",@"girl", nil];
    [mutArray removeObject:@"sd"];
    
      //1.2按下标删除
     [mutArray removeObjectAtIndex:3];
    
      //1.3删除最后一个
      [mutArray removeLastObject];
    
      //删除多个
      //在range范围内删除第一个参数的中的数据
      NSIndexSet *set4 = [[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(5, 2)];
      [mutArray removeObjectsAtIndexes:set4];
    
      //删除全部
      [mutArray removeAllObjects];
    

    3.字符传切割

    //切割
    NSString *str = @"sdjfkjdsjf#*sjkd#*skdud#*skudjhfksd#*";
    NSArray *array = [str componentsSeparatedByString:@"#*"];
    
    //一字符集合中的字符切割
    NSString *str1 =@"skjdfsl!skljdfkds* smjdfjs*JHhndnf.sdfs";
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"!*,."];
    NSArray *arr1 = [str1 componentsSeparatedByCharactersInSet:set];
    
    //组合
    //将数组中的字符传元素按照某种规则组合成一个新的字符串
    NSArray *arr3 = @[@"sdjn",@"sun",@"id",@"sd"];
    NSString *str4 = [arr3 componentsJoinedByString:@"!!"];
    

    4.简单数据封装存储在字典和数字中

    //NSNumber:专门用于封装简单数据类型的类
    //Why:在OC中数组、字典等存储工具里面只能存储对象,所以需要封装
    //封装成对象
    int a = 10;
    int b = 20;
    
    NSNumber *aNum = [[NSNumber alloc] initWithInt:a];
    NSNumber *bNum = [[NSNumber alloc] initWithInt:b];
    
    NSLog(@"aNum = %@", aNum);
    NSLog(@"bNum = %@", bNum);
    
    //存入数组
    NSArray *array = @[aNum, bNum];
    
    //比较大小
    NSComparisonResult result = [aNum compare:bNum];
    
    //解封
    int c = [aNum intValue];
    int d = [bNum intValue];
    

    5.数组排序

    //数组的排序
    //写排序的标准(方法):(升序排、降序排),写在数组元素的类里面
    //NSOrderedDescending  NSOrderedAscending
    //带SEL的排序
    [arr  sortUsingSelector:NSOrderedDescending];
    

    数组排序1

    void arraySort1() {  
        NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil nil];  
      
        // 返回一个排好序的数组,原来数组的元素顺序不会改变  
        // 指定元素的比较方法:compare:  
        NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];  
        NSLog(@"array2:%@", array2);  
    }  
    

    数组排序2

    void arraySort2() {  
      Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];  
      Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];  
      Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];  
      Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];  
      NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil];  
      // 指定排序的比较方法  
      NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];  
      NSLog(@"array2:%@", array2);  
    }  
    - (NSComparisonResult)compareStudent:(Student *)stu {  
      // 先按照姓排序  
      NSComparisonResult result = [self.lastname compare:stu.lastname];  
      // 如果有相同的姓,就比较名字  
      if (result == NSOrderedSame) {  
          result = [self.firstname compare:stu.firstname];  
      }  
      return result;  
    }  
    

    数组排序3

    void arraySort3() {  
      Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];  
      Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];  
      Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];  
      Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];  
      NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil];  
      
      // 利用block进行排序  
      NSArray *array2 = [array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {  
         // 先按照姓排序  
         NSComparisonResult result = [obj1.lastname compare:obj2.lastname];  
         // 如果有相同的姓,就比较名字  
         if (result == NSOrderedSame) {  
             result = [obj1.firstname compare:obj2.firstname];  
         }  
           
         return result;  
      }];  
      
      NSLog(@"array2:%@", array2);  
    }  
    

    数组排序4-高级排序

    void arraySort4() {  
      Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"];  
      Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"];  
      Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"];  
      Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"];  
      NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil];  
      
      // 1.先按照书名进行排序  
      // 这里的key写的是@property的名称  
      NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];  
      // 2.再按照姓进行排序  
      NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];  
      // 3.再按照名进行排序  
      NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];  
      // 按顺序添加排序描述器  
      NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil nil];  
      
      NSArray *array2 = [array sortedArrayUsingDescriptors:descs];  
      
      NSLog(@"array2:%@", array2);  
    }  

    相关文章

      网友评论

          本文标题:iOS 数组方法大全

          本文链接:https://www.haomeiwen.com/subject/twojlqtx.html