Objective-C语法-数组NSArray和可变数组NSMutableArray

来源:互联网 发布:淘宝买药多久发货 编辑:程序博客网 时间:2024/06/10 04:45

       Objective-C的数组比C++Java的数组强大在于,NSArray保存的对象可以是不同的对象。但只能保存对象,int ,char,double等基本数据类型不能直接保存,需要通过转换成对象才能加入数组。

       NSArray NSMutableArray  二者有时可任意选用,有时又必区分开来

       NSArray只能存储Objective-C对象,而不能存储C语言中的基本数据类型,比如:intflot,指针等。在使用NSArray时,需特别注意,必须以nil结尾。以此来代表结束. NSArray 创建的是静态数组,一旦创建之后,就再也不能添加和删除数组中的对象了。

       NSMutableArray NSArray补充类。NSMutalbeArray创建的是动态数组,可随意添加或删除数组中的元素。

       NSMutableArray使用 addObject:在末尾添加对象, removeObjectAtIndex来删除指定索引处的对象。对象删除后,被删除对象后面的数组元素被迁移,填补空缺。


一.NSArray






1.NSArray初始化赋值 OC数组内只能存放对象

初始化的几种方式,可以一个元素、多个元素、从已有组复制、从文件、网址等

    NSArray *arr1 = [NSArray arrayWithObject:@"hello"];    NSArray *arr2 = [NSArray arrayWithObjects:@"hello1",@"hello2",@"hello3", nil];//最基本初始化    NSArray *arr3 = @[@"hello1",@"hello2",@100, @200, @300];    NSArray *arr4 = [NSArray arrayWithArray:arr1];//复制    //NSArray *arr5 = [NSArray arrayWithContentsOfFile:<#(NSString *)#>];    //NSArray *arr6 = [NSArray arrayWithContentsOfURL:<#(NSURL *)#>];

获取数组的个数

    NSInteger count = [arr2 count];    NSLog(@"count = %ld",(long)count);

根据下标访问里面的对象

    NSString *arrObj = [arr2 objectAtIndex:1];    NSLog(@"arrObj = %@",arrObj);

 给数组增减新元素(对象)

    NSArray *arrNew = [arr2 arrayByAddingObject:@"hello4"];    NSLog(@"arrNew = %@",arrNew);

把数组里元素利用连接符连接成字符串

    NSString *strNew = [arr2 componentsJoinedByString:@"-"];    NSLog(@"strNew = %@",strNew);

查询数组中是否包含某一元素,YESNO

    BOOL isContain = [arr2 containsObject:@"hello2"];    if (isContain) {        NSLog(@"存在"); }    else        NSLog(@"不存在");

查询并返回位置,如没有则是NSNotFound

    NSInteger index = [arr2 indexOfObject:@"hello2"];    NSLog(@"index = %ld",(long)index);

取出收尾元素

    NSString *lastStr = [arr2 lastObject];//取出最后一个元素    NSLog(@"%@",lastStr);    NSString *firstStr = [arr2 firstObject];//取出第一个元素    NSLog(@"%@",firstStr);


数组的遍历(查看数组中每个元素)

1:基本的for循环通过下标注意取出查看

2:for in 快速枚举

    //1:基本的for循环通过下标注意取出查看    for (int i = 0; i < arr2.count; i++) {        NSString *str = [arr2 objectAtIndex:i];        NSLog(@"str = %@",str);    }        //2:for in 快速枚举(我们需要让数组中元素类型保持一致);    //str2会去找到arr2中所有的字符串,每循环一次找到一个    for (NSString *str2 in arr2) {        NSLog(@"str2 = %@",str2);    }        //当不确定元素类型时用id,比如除了NSString还有NSNumber    for (id str3 in arr2) {        NSLog(@"str3 = %@",str3);    }



二.NSMutableArray





    //可变数组,NSMutableArray        //初始化    NSMutableArray *mutArr = [[NSMutableArray alloc]initWithCapacity:10];    NSMutableArray *mutArr2 = [NSMutableArray arrayWithObjects:@"hello1",@"hello2", nil];        //添加一个元素(初始化)    [mutArr addObject:@"hello1"];    [mutArr addObject:@"hello3"];    [mutArr addObject:@"hello4"];    NSLog(@"mutArr = %@",mutArr);        //指定位置插入一个元素    [mutArr insertObject:@"hello2" atIndex:1];    NSLog(@"mutArr = %@",mutArr);        //替换元素    [mutArr replaceObjectAtIndex:1 withObject:@"hello"];    NSLog(@"mutArr = %@",mutArr);        //交换元素位置  0和2  交换    [mutArr exchangeObjectAtIndex:0 withObjectAtIndex:2];    NSLog(@"mutArr = %@",mutArr);            //把一个数组添加到另一个里    [mutArr addObjectsFromArray:mutArr2];     NSLog(@"mutArr = %@",mutArr);        //移除数组的元素,按名称按序号等    [mutArr removeLastObject];//移除最后一个    [mutArr removeObject:@"hello2"];//移除hello2元素    [mutArr removeObjectAtIndex:0];//移除索引为1的元素    [mutArr removeAllObjects];//移除全部    NSLog(@"mutArr = %@",mutArr);


附:本文Demo

http://download.csdn.net/detail/jackjia2015/9408829



1 0
原创粉丝点击