NSArray NSNutableArray

    xiaoxiao2026-08-20  0

    NSArray NSNutableArray

    NSArray特点:

    一旦创建成功,内容不可改变 只能存放OC对象

    创建数组

    1.创建一个空数组

    NSArray *arr1 = [NSArray array];

    2.创建数组,只有一个元素

    NSArray *arr2 = [NSArray arrayWithObject:@"1"];

    3.创建数组,有多个元素 nil 表示数组赋值结束 常见写法

    NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1, nil]; NSLog(@"arr3 = %@",arr3);

    4.调用对象方法,创建数组

    NSArray *arr4 = [[NSArray alloc] initWithObjects:@"three",[NSNull null],@"four", nil]; NSLog(@"arr4 = %@",arr4);

    5.用一个数组可以创建另外一个数组

    NSArray *arr5 = [NSArray arrayWithArray:arr3];

    NSArray常用方法

    1.获取数组的长度 count获取数组的元素的个数

    NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1,@"three", nil]; NSLog(@"arr3 = %@",arr3); NSLog(@"%ld",arr3.count);

    2.根据下标,获取下标对应的对象

    NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1,@"three", nil]; NSLog(@"arr3 = %@",arr3); NSLog(@"%@",[arr3 objectAtIndex:3]);

    3.返回元素的下标

    NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1,@"three", nil]; NSLog(@"arr3 = %@",arr3); NSUInteger loc = [arr3 indexOfObject:@"three"]; NSLog(@"%ld",loc);

    4.数组中是否包含了某个元素

    NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1,@"three", nil]; NSLog(@"arr3 = %@",arr3); if([arr3 containsObject:@"four"]){ NSLog(@"包含此元素"); }else{ NSLog(@"不包含"); } NSLog(@"arr5 = %@",arr5);

    NSArray简化形式

    1.用简化的方式,来定义和访问数组元素 (1)用简化的方式,定义数组 格式: @[ 数组元素 ]

    NSArray *arr = @[@"1",@"one",@"3",@4,@"ONE"]; NSLog(@"arr = %@",arr); NSString *str =[arr objectAtIndex:2]; NSLog(@"%@",str);

    (2)用简化的方式访问数组元素

    str = arr[1]; //C语言形式的数组元素访问 NSLog(@"%@",str);

    NSArray的遍历

    //定义一个数组 NSArray *arr = @[@"one",@"two",@"three",@"four"];

    1.对数组进行遍历

    (1). 普通的方式,通过下标访问

    for (int i=0; i<arr.count; i++) { NSLog(@"-> %@",arr[i]); }

    (2). 快速枚举法 for循环的增强形式

    for (NSString * str in arr) { NSLog(@"---> %@",str); }

    (3). 使用block的方式,进行访问

    // 数组元素 元素下标 是否停止 //stop:YES 会停止, stop:NO 不会停止 [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if(idx == 2){ *stop = YES; //停止 // break; }else{ NSLog(@"idx = %ld,obj = %@",idx,obj); } }];

    NSArray与字符串:

    把数组拼接成为字符串
 //定义一个数组 NSArray *arr = @[@1,@2,@3,@4];

    (1)需求: 把数组中的元素用 “-” 连接起来 [数组 componentsJoinedByString @”分隔符”];

    // 1-2-3-4 NSString *str = [arr componentsJoinedByString:@"-"]; NSLog(@"str = %@",str); //把字符串拆分为数组

    (2) 给一个字符串,分割成一个数组 // 400-800-12580 //取得 400 12580 800

    NSString *str2 = @"400-800-12580"; NSArray *arr2 = [str2 componentsSeparatedByString:@"-"]; NSLog(@"%@",[arr2 firstObject]); NSLog(@"%@",[arr2 lastObject]); NSLog(@"%@",arr2[1]);

    NSArray的用法:

    第一、初始化

    NSArray *firstArray=[[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil]; NSArray *secondArray=[NSArray arrayWithArray:firstArray];

    获取元素个数和访问

    NSLog(@"the number is %ld",[secondArray count]); NSLog(@"the value is %@",[secondArray objectAtIndex:2]);

    追加数据元素

    NSArray *thirdArray=[firstArray arrayByAddingObjectsFromArray:secondArray];

    数组转化为字符串

    NSString *str=[firstArray componentsJoinedByString:@".."]; NSLog(@"the number is %@",str);

    判断是否包含字符串

    NSArray *firstArray=[[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil]; NSLog(@"has value %d",[firstArray containsObject:@"two"]); NSLog(@"has value %ld",[firstArray indexOfObject:@"two"]); NSLog(@"the last object is %@",[firstArray lastObject]);

    NSMutalbeArray 的用法

    基本的增删改

    NSMutableArray *mutableArr=[NSMutableArray arrayWithCapacity:4]; [mutableArr addObject:@"hello"]; [mutableArr addObject:@"hello"]; [mutableArr addObject:@"hello"];

    插入

    [mutableArr insertObject:@"yang" atIndex:1]; NSLog(@"%@",mutableArr);

    删除

    [mutableArr removeObject:@"hello"]; [mutableArr removeObjectAtIndex:0]; [mutableArr removeLastObject]; NSLog(@"%@",mutableArr);
    替换
    [mutableArr replaceObjectAtIndex:0 withObject:@"kaixin"];
    转载请注明原文地址: https://ju.6miu.com/read-1311395.html
    最新回复(0)