码迷,mamicode.com
首页 > 其他好文 > 详细

NSArray和NSMutableArray的基本用法

时间:2015-04-22 20:29:49      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

 /*---------------------NSArray---------------------------*/
        //创建数组
        NSArray *array1 = [NSArray arrayWithObject:@"1"];
        NSArray *array2 = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
        NSArray *array3 = [NSArray arrayWithArray:array2];
        NSLog(@"array1 = %@",array1);
        NSLog(@"array2 = %@",array2);
        NSLog(@"array3 = %@",array3);
        
        //获取数组内数据个数
        int count = [array2 count];
        NSLog(@"array2 size is %d",count);
        
        //访问数组内的数据
        NSString *str1 = [array2 objectAtIndex:0];
        NSLog(@"array2 first content is %@",str1);
        
        //数组中插入数据   返回新的数组
        NSArray *array4 = [array3 arrayByAddingObject:@"4"];
        NSLog(@"array4 = %@",array4);
        
        //数组内的数据以制定字符连接
        NSString *str2 = [array4 componentsJoinedByString:@","];
        NSLog(@"str2 = %@",str2);
        
        //判断数组中是否包含某对象
        BOOL b1 = [array4 containsObject:@"4"];
        BOOL b2 = [array4 containsObject:@"5"];
        NSLog(@"b1 = %d,b2 = %d",b1,b2);
        
        //取数组内制定对象的索引
        int index = [array4 indexOfObject:@"4"];
        NSLog(@"index = %d",index);
        
        NSString *str3 = [array4 lastObject];
        NSLog(@"array4 last object is %@",str3);
        
        /*-----------------------可变数组NSMutableArray-----------------------------------------*/
        //初始化数组 指定数组长度   但可变
        NSMutableArray *mArray1 = [NSMutableArray arrayWithCapacity:5];
        
        //向数组中添加元素
        [mArray1 addObject:@"aaaa"];
        [mArray1 addObject:@"cccc"];
        NSLog(@"mArray1 = %@",mArray1);
        
        //向指定位置插入元素
        [mArray1 insertObject:@"bbbb" atIndex:1];
        [mArray1 insertObject:@"dddd" atIndex:[mArray1 count]];
        [mArray1 insertObject:@"eeee" atIndex:[mArray1 count]];
        NSLog(@"mArray1 = %@",mArray1);
        
        //移除元素
        [mArray1 removeObject:@"eeee"];
        NSLog(@"mArray1 = %@",mArray1);
        [mArray1 removeObjectAtIndex:[mArray1 count]-1];
        NSLog(@"mArray1 = %@",mArray1);
        NSArray *array5 = [NSArray arrayWithObjects:@"bbbb",@"cccc", nil];
        [mArray1 removeObjectsInArray:array5];
        NSLog(@"mArray1 = %@",mArray1);
        
        //向数组内插入数组
        NSMutableArray *mArray2 = [NSMutableArray arrayWithObjects:@"aaaa",@"aaaa", nil];
        [mArray2 addObject:@"bbbb"];
        NSLog(@"mArray2 = %@",mArray2);
        [mArray2 addObjectsFromArray:array4];
        NSLog(@"mArray2 = %@",mArray2);
        
        //替换元素
        [mArray2 replaceObjectAtIndex:[mArray2 count]-1 withObject:@"5"];
        NSLog(@"mArray2 = %@",mArray2);
        
        //遍历数组  常规方法:性能较低
        NSArray *array6 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e", nil];
        int len = [array6 count];
        for (int i=0; i<len; i++) {
            NSString *value = [array6 objectAtIndex:i];
            NSLog(@"array6 %d content is %@",i,value);
        }
        
        //枚举遍历  相当于java中的增强for循环
        for (NSString *string in array6) {
            NSLog(@"array6 content is %@",string);
        }
        
        NSLog(@"-----------------------");
        
        //当不确定数组元素类型时
        for(id string in array6){
            NSLog(@"array6 content is %@",string);
        }

  

NSArray和NSMutableArray的基本用法

标签:

原文地址:http://www.cnblogs.com/song-kl/p/4448484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!