OC—数组

来源:互联网 发布:tensorflow官网下载 编辑:程序博客网 时间:2024/06/14 21:36

一.不可变数组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
原创粉丝点击