03 OC之数组NSArray


/****************  不可变数组   Immutable Array****************/

1 、数组的创建

  •   NSString *s1 =@"hello";
  •     NSString *s2 = @"good";
  •     NSString *s3 = @"morning";
   
  •     //注意最后的 nil
  •     NSArray *array1 = [[NSArrayalloc] initWithObjects:s1,s2,s3, nil];
  •     NSLog(@"array1 = %@",array1.description);//等价于[array1 descriptiion]
    •        description 格式 先写上
  • //    //类方法的创建
  • //    NSArray *array2 = [[NSArray arrayWithObjects:s2,s3, nil]];
  •     
  •     //创建数组的同时,往数组里存入一个 元素
  •     NSArray *array3 = [NSArrayarrayWithObject:s3];    
  •     //二维数组的创建
  •     NSArray *array4 = [NSArrayarrayWithObjects:array1,array3, nil];    
2 、通过下标获取元素
  •     NSString *str1 = [array1objectAtIndex:1];    
3 、数组的元素个数
  •     NSUInteger count1 = [array1count];
4 、判断数组中是否包含了 某一元素
  •     BOOL beContain = [array1containsObject:@"good"];    
5 、查找一个对象在数组中的下标   index 索引;指标;这里表示 下标
  •     NSUInteger index1 = [array1indexOfObject:@"good"]; //只用于整个元素,只写goo则没有
  •     if (index1 == NSNotFound) {
  •         NSLog(@"arry1中没有这个");
  •     }else {
  •         NSLog(@"index = %ld", index1);
  •     }
   
6 、使用连接符,将数组中的元素连接起来 : componentsJoinedByString
  •   NSString *joinString = [array1componentsJoinedByString:@"-"];    
  •     //字符串的分裂    componentsSeparatedByString
  •     NSString *str2 = @"sfwefweiofh iowh foirehawrhg oirhiorh owh tohr4 olw";
  •     NSArray *separate = [str2 componentsSeparatedByString:@" "];    
7 、访问数组最后一个元素
  •   NSString *lastObject = [separatelastObject];
  •     NSString *lastStr = [separate objectAtIndex:(separate.count - 1)];//用最后一个下标访问
8 、追加元素
  •   NSArray *array5 = [array1arrayByAddingObject:@"Rodolfo"];
  •     NSLog(@"array5 = %@", array5.description);
  •     
  •  /*
  • 1、数组中只能存放对象,不能存放基本数据类型 1 2 3
  •      
  • 2、数组越界 reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 2]'
  • // 数组越界
  • //    NSString *str3 = [array1 objectAtIndex:5];//错误
  • 3、数组中存放的对象类型要一致
  • */
  •     
  •     //严谨的数组取值格式
  •     int inde = 3;
  •     if (inde >= 0 && inde <= array5.count - 1) {
  •         NSString *str4 = [array5 objectAtIndex:inde];
  •         NSLog(@"严谨写法\n str4 = %@",str4);
  •     }

   
//———————————————————— 数组的迭代(遍历) ——————————————————————
1 、普通迭代 遍历
  •     NSString *str5;
  •     for (int i =0; i < array5.count; i++) {
  •         str5 = [array5 objectAtIndex:i];
  •         NSLog(@"str5 = %@",str5);
  •     }
2 、快速遍历     
  • 如果需要在便利中使用下标,则不能用 快速 遍历
  •     for(NSString*str6 in array1) {
  •         NSLog(@"str6 = %@",str6);
  •     }
****************    Mutable Array   ****************

1、可变数组的创建
  • NSMutableArray *mtArray1 = [[NSMutableArray alloc]initWithObjects:str1,@“string*”,str2,nil];
  • [[NSMutableArray alloc]initWithCapacity:5]; // 5表示 开辟容量为5的空间,
  • //但当储存元素超过5个时,数组会自动扩大 
2、添加元素 : [mArray addObject:str1];
  • 将一个数组中的元逐个增加到另一数组
  • [A addObjectsFromArray:B]; 
  • 将数组A 作为元素 添加到数组B中
  • [A addObject:B];
3、插入元素   
  • [A insertObject:str atIndex:index];//str是要插入的字符串,index是下标
4、替换元素
  • [A replaceObjectAtIndex:下标 withObject:字符串];
5、交换元素
  • [A exchangeObjectAtIndex:下标1 withObjectAtIndex:下标2];
6、删除元素
  • 根据下标删除
    • [A removeObjectAtIndex:下标];
  • 删除最后一个
    • [A removeLastObject];
  • 删除指定对象
    • [A removeObject:@“某元素”];
  • 全部删除
    • [A removeAllObjects];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值