OC—数组

一.不可变数组NSArray

1. NSArray对象的创建

NSArray *arr1 = [[NSArray alloc] initWithObjects:@"hello",@"1234", @"2345", @"world", nil];
NSArray *arr11 = [NSArray arrayWithObjects:@"1234",@"2345", nil];
     NSLog(@"%@", arr1);
     NSLog(@"%@", arr11);

    //用一个已经存在对数组实例化一个新数组
     NSArray *arr2 = [[NSArray alloc] initWithArray:arr1];
     NSLog(@"%@", arr2);
    
     NSArray *arr3 = @[@"hehe", @"123"];
     NSLog(@"%@", arr3);


2. NSArray的常用方法

    

NSUInteger length = [arr3 count];
//获取最后一个元素
    NSLog(@"%@", [arr3 lastObject]);
    //获取第一个元素
    NSLog(@"%@", [arr3 firstObject]);

3. NSArray的遍历

//下标遍历
    for (NSInteger i = 0; i<length; i++) {
        //objectAtIndex的返回值是id
        NSString *str = [arr3 objectAtIndex:i];
        NSLog(@"%@",str);
    }
    //快捷便利
    for (NSString *str in arr3) {
        NSLog(@"%@", str);
    }
    //类c访问方式
    NSLog(@"%@", arr3[0]);


二.NSMutableArray常用方法

//创建
NSMutableArray *marr = [[NSMutableArray alloc]init];
    NSLog(@"marr1=%p", marr);
//添加对象方式一
    [marr addObject:@“One"];
//添加对象方式二
    NSArray * tarr = @[@"One",@"Two",@"Three",@"four", @"five"];
    [marr setArray:tarr];
    NSLog(@"marr=%@", marr);
    
    NSString *res = marr[2];
    NSLog(@"%@", res);
//插入元素
    [marr insertObject:@"tttt" atIndex:3];
    NSLog(@"marr=%@", marr);
//x删除元素  
    [marr removeObject:@"tttt"];
    NSLog(@"marr=%@", marr);
    [marr removeObjectAtIndex:0];
    NSLog(@"marr=%@", marr);
    [marr removeObject:@"four" inRange:NSMakeRange(0, 3)];
    NSLog(@"marr=%@", marr);
    [marr removeAllObjects];
    NSLog(@“%@“,marr);

    NSMutableArray * arr = [NSMutableArray arrayWithArray:@[@"00",@"11",@"22",@"33",@"44",@"55",@"66",@"77"]];
    
    //交换两个元素
    [arr exchangeObjectAtIndex:2 withObjectAtIndex:3];
    NSLog(@"%@", arr);
    //替换
    [arr replaceObjectAtIndex:0 withObject:@"30"];
    NSLog(@"%@", arr);

//冒泡排序
    for (int i=0; i<[arr count]; i++) {
        for (int j=i; j<[arr count]-1; j++) {
            if ([arr[i] integerValue]>[arr[j] integerValue]) {
                [arr exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
    NSLog(@"%@", arr);
    
    //排序<<选择器>>
    NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"F",@"B",@"C",@"D",@"E", nil];
    NSArray *arr3 = [arr2 sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@“%@“,arr2);//不变
    NSLog(@"%@", arr3);

//    [arr2 sortUsingSelector:@selector(compare:)];//arr2变了
//sel:方法选择器,功能就是存储一个方法名
    //根据提供的方法,逐个比较数组里面的元素
    //限制条件:数组里面的元素所属的类要包含提供方法


题目:
创建一个数组,数组中包含若干不相同的字符串,要求将下标为3的倍数的元素全部删除
+(NSMutableArray*) remove3:(NSMutableArray*)arr{
    //方法一
   
//    NSMutableArray *tmp = [[NSMutableArray alloc] init];
//
//    
//    for(NSInteger i=0; i<[arr count]; i++){
//        if(i%3==0){
//            [tmp addObject:arr[i]];
//        }
//    }
//    
//可替换为removeObjectsInArray
//    for (id obj in tmp) {
//        [arr removeObject:obj];
//    }
    
    //方法二
//    NSMutableArray *tmp = [[NSMutableArray alloc] init];
//
//    for(NSInteger i=0; i< [arr count]; i++){
//        if(i%3!=0){
//            [tmp addObject:arr[i]];
//        }
//    }
//    return tmp;
    
    //方法三
    NSMutableArray *tmp = [NSMutableArray arrayWithArray:arr];
    NSLog(@"tmp=%@", tmp);
    for (NSInteger i=0; i<[tmp count]; i++) {
        if (i%3==0) {
            [arr removeObject:tmp[i]];
        }
    }
   
    return arr;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值