国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS開發之OC篇(3)—— NSArray和NSMutableArray

2019-11-09 15:39:32
字體:
來源:轉載
供稿:網友

版本

Xcode 8.2.1

一、不可變數組NSArray

老規矩,先貼上知識點,再打個例子和舉個比方。    

//使用arrayWithObjects創建NSArray

    NSArray *arr = [NSArray arrayWithObjects:@"abc",@"def",@"123",nil];

//    NSArray *arr = [[NSArray alloc] initWithObjects:@"111",@"ddd",@"234",nil];

    

    //使用arrayWithArray創建NSArray

    NSArray *arr1 = [NSArray arrayWithArray:arr];

//    NSArray *arr1 = [[NSArray alloc]initWithArray:arr];

    

    //使用@創建NSArray,只能創建不可變數組

    NSArray *arr2 = @[@"aaaa",@"bbb",@"ddddd"];

    NSLog(@"arr2 = %@",arr2);

    

    //返回元素個數

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

    

    //返回指定位置id

    NSString *str0 = [arr2 objectAtIndex:2];

    NSLog(@"str0 = %@",str0);

    

    //訪問數組的最后一個元素

    NSString *str = [arr2 lastObject];

    NSLog(@"str = %@",str);

    

    //訪問數組的第一個元素

    NSString *str1 = [arr firstObject];

    NSLog(@"str1 = %@",str1);

    

    //判斷數組內是否含有某一個對象

    if([arr containsObject:@"123"]) {

        NSLog(@"含有此對象");

    }

    

    //獲取某個對象在數組中的下標值

    NSUInteger index = [arr indexOfObject:@"123"];

    

    //數組的遍歷

    for(NSInteger i =0;i < [arr count]; i++) {

        NSLog(@"arr[%ld] = %@",i,arr[i]);

    }

    

    //快速枚舉法(快速遍歷)

    for(id objin  arr) {

        NSLog(@"obj = %@",obj);

    }

int main(int argc, char * argv[]) {        //使用arrayWithObjects創建NSArray    NSArray *arr = [NSArray arrayWithObjects:@"ABC",@"DEF",@"GHI",nil];        //NSArray *arr = [[NSArray alloc] initWithObjects:@"ABC",@"DEF",@"GHI",nil];    NSLog(@"arr = %@",arr);        //使用arrayWithArray創建NSArray    NSArray *arr1 = [NSArray arrayWithArray:arr];        //NSArray *arr1 = [[NSArray alloc]initWithArray:arr];    NSLog(@"arr1 = %@",arr1);        //使用@創建NSArray,只能創建不可變數組    NSArray *arr2 = @[@"123",@"456",@"789"];    NSLog(@"arr2 = %@",arr2);        //返回元素個數    NSLog(@"arr count = %ld",[arr count]);        //返回指定位置id    NSString *str = [arr objectAtIndex:1];    NSLog(@"str = %@",str);        //訪問數組的第一個元素    NSString *strF = [arr firstObject];    NSLog(@"strF = %@",strF);        //訪問數組的最后一個元素    NSString *strL = [arr lastObject];    NSLog(@"strL = %@",strL);        //判斷數組內是否含有某一個對象    if ([arr2 containsObject:@"456"]) {        NSLog(@"數組arr2內含有對象456");    }        //獲取某個對象在數組中的下標值    NSUInteger index = [arr indexOfObject:@"ABC"];    NSLog(@"ABC下標值為%ld",index);        //數組的遍歷    for (NSInteger i = 0; i < [arr count]; i++) {        NSLog(@"arr[%ld] = %@",i,arr[i]);    }        //快速枚舉法(快速遍歷)    for (id arr2Q in arr2) {        NSLog(@"arr2Q = %@",arr2Q);    }}輸出結果:

二、可變數組NSMutableArray

    //實例化一個空的可變數組

    NSMutableArray *nsarr = [[NSMutableArray alloc]init];

    

    //添加一個元素

    [nsarr addObject:@"ONE"];

    NSLog(@"nsarr = %@",nsarr);

    

    //添加多個元素

    [nsarr addObjectsFromArray:@[@"ONE",@"TWO",@"THREE"]];

    NSLog(@"nsarr = %@",nsarr);

    

    //插入元素

    [nsarr insertObject:@"Two" atIndex:1];

    NSLog(@"nsarr = %@",nsarr);

    

    //替換元素

    [nsarr replaceObjectAtIndex:0 withObject:@"1"];

    NSLog(@"nsarr = %@",nsarr);

    

    //查詢元素

    NSString *result = [nsarr objectAtIndex:1];//arr[1]

    NSLog(@"result = %@",result);

    

    //刪除指定元素

    [nsarr removeObject:@"ONE"];

    NSLog(@"nsarr = %@",nsarr);

    

    //刪除指定下標的元素

    [nsarr removeObjectAtIndex:2];

    NSLog(@"nsarr = %@",nsarr);

    

    //刪除最后一個元素

    [nsarr removeLastObject];

    NSLog(@"nsarr = %@",nsarr);

    

    //刪除所有元素

    [nsarr removeAllObjects];

    NSLog(@"nsarr = %@",nsarr);

    

    //刪除數組中含有的所有元素

    [nsarr removeObjectsInArray:@[@"1",@"THREE"]];

    

    //交換元素

    NSMutableArray *nsarr1 = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];

    [nsarr1 exchangeObjectAtIndex:1 withObjectAtIndex:2];

    NSLog(@"nsarr1 = %@",nsarr1);

    

    //切割字符串

    NSString *str3 = @"I am a super man + beautiful, yeah!";

    //用指定的字符串進行切割

    NSArray *rArr = [str3 componentsSeparatedByString:@" "];

    NSLog(@"rArr = %@",rArr);

    

    //實例化一個字符集合

    NSCharacterSet *set = [NSCharacterSet  characterSetWithCharactersInString:@" ,+"];

    //按照字符集合所提供的字符進行切割

    NSArray *rArr2 = [str3 componentsSeparatedByCharactersInSet:set];

    NSLog(@"rArr2 = %@",rArr2);

    

    //拼接數組內的字符串

    NSString *str2 = [rArr2 componentsJoinedByString:@"-"];

    NSLog(@"str2 = %@",str2);

    

    //按照ASCII碼大小進行排序,SEL:方法選擇器(儲存一個方法名)

    NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"B",@"A",@"C",@"Z",nil];

    [arr3 sortedArrayUsingSelector:@selector(compare:)];//方法選擇器

    

    //按三個人年齡大小排序

    Person *per0 = [[Person alloc]initWith:18];

    Person *per1 = [[Person alloc]initWith:6];

    Person *per2 = [[Person alloc]initWith:60];

    NSMutableArray *perArr = [[NSMutableArray alloc]initWithObjects:per0,per1,per2,nil];

    //compareAge提供一個排序方法,看按什么方式排序

    NSArray *resultPerArr = [perArr sortedArrayUsingSelector:@selector(compareAge:)];

int main(int argc, char * argv[]) {        //實例化一個空的可變數組    NSMutableArray *arr = [[NSMutableArray alloc]init];        //添加一個元素    [arr addObject:@"he"];    NSLog(@"arr = %@",arr);        //添加多個元素    [arr addObjectsFromArray:@[@"is",@"Chinese"]];    NSLog(@"arr = %@",arr);    //插入元素    [arr insertObject:@"not" atIndex:2];    NSLog(@"arr = %@",arr);        //以arrayWithArray創建arr1    NSMutableArray  *arr1 = [NSMutableArray arrayWithArray:arr];    //替換元素    [arr replaceObjectAtIndex:3 withObject:@"Japanese"];    NSLog(@"arr = %@",arr);    //查詢元素    NSString *result = [arr objectAtIndex:0];    NSLog(@"arr[0] = %@",result);    //刪除指定元素    [arr removeObject:@"not"];    NSLog(@"arr = %@",arr);    //刪除指定下標的元素    [arr removeObjectAtIndex:1];    NSLog(@"arr = %@",arr);    //刪除最后一個元素    [arr removeLastObject];    NSLog(@"arr = %@",arr);    //刪除所有元素    [arr removeAllObjects];    NSLog(@"arr = %@",arr);    //刪除數組中含有的所有元素    [arr1 removeObjectsInArray:@[@"not",@"is"]];    NSLog(@"arr1 = %@",arr1);    //交換元素    [arr1 exchangeObjectAtIndex:0 withObjectAtIndex:1];    NSLog(@"arr1 = %@",arr1);    //切割字符串    NSString *str1 = @"I am,a super man!";    //用指定的字符串進行切割    NSArray *cutArr = [str1 componentsSeparatedByString:@","];    NSLog(@"cutArr = %@",cutArr);    //實例化一個字符集合    NSCharacterSet *set = [NSCharacterSet  characterSetWithCharactersInString:@"super "];    //按照字符集合所提供的字符進行切割    NSArray *cutArr2 = [str1 componentsSeparatedByCharactersInSet:set];    NSLog(@"cutArr2 = %@",cutArr2);    //拼接數組內的字符串    NSString *str2 = [cutArr2 componentsJoinedByString:@"-"];    NSLog(@"str2 = %@",str2); /*    //按照ASCII碼大小進行排序,SEL:方法選擇器(儲存一個方法名)    NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"B",@"A",@"C",@"Z",nil];    [arr3 sortedArrayUsingSelector:@selector(compare:)]; //方法選擇器        //按三個人年齡大小排序    Person *per0 = [[Person alloc]initWith:18];    Person *per1 = [[Person alloc]initWith:6];    Person *per2 = [[Person alloc]initWith:60];    NSMutableArray *perArr = [[NSMutableArray alloc]initWithObjects:per0,per1,per2,nil];    //compareAge提供一個排序方法,看按什么方式排序    NSArray *resultPerArr = [perArr sortedArrayUsingSelector:@selector(compareAge:)]; */}輸出結果:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鹤壁市| 乐都县| 元江| 许昌县| 锡林浩特市| 河间市| 绥阳县| 平昌县| 鹤岗市| 临桂县| 丰原市| 潮州市| 伊吾县| 洮南市| 辽阳县| 盘山县| 阳高县| 肥乡县| 维西| 荃湾区| 麻江县| 平原县| 张掖市| 北宁市| 镇远县| 乳源| 梁河县| 文安县| 九江市| 望城县| 凤庆县| 环江| 腾冲县| 宁强县| 天全县| 垫江县| 科技| 淄博市| 区。| 英山县| 镇康县|