iOS中NSArray數組常用處理方式_IOS

來源:互聯網
上載者:User

1. 數組的常用處理方式

//--------------------不可變數組//1.數組的建立NSString *s1 = @"zhangsan";NSString *s2 = @"lisi";NSString *s3 = @"wangwu";//(1)NSArray *array1 = [[NSArray alloc] initWithObjects:s1,s2,s3, nil];NSLog(@"%@",array1); //等價於 array1.descripton//(2)使用類方法建立NSArray *array2 = [NSArray arrayWithObjects:s1,s2,s3, nil];//(3)建立一個數組對象,同時往裡面存入一元素NSArray *array3 = [NSArray arrayWithObject:s1];//(4)建立一個數組,此數組中的元素來自array1NSArray *array4 = [NSArray arrayWithArray:array1];NSLog(@"array4 = %@",array4);//2.通過下標取元素NSString *str1 = [array4 objectAtIndex:0];//3.數組元素的個數NSUInteger count = [array4 count]; //等價於:array4.count;//4.判斷數組中的是否包含某個元素BOOL isContains = [array4 containsObject:@"zhangsan"];NSLog(@"isContains:%d",isContains);//5.尋找某一個對象在數組中的下標位置NSUInteger index = [array4 indexOfObject:@"wangwu"];if (index == NSNotFound) {NSLog(@"Not find elemnts");} else {NSLog(@"index = %ld",index);}//6.連結數組中的字串(前提:數組中都是字串)NSString *joinString = [array4 componentsJoinedByString:@","];NSLog(@"joinString = %@",joinString);//7.訪問數組最後一個元素NSString *lastObj = [array4 lastObject]; //array4.lastObjectNSLog(@"lsatObj = %@",lastObj);//8.再原路的數組後面添加一個元素NSArray *array5 = [array4 arrayByAddingObject:@"zhaolia"];NSLog(@"array5 = %@",array5);//取對應下標元素int idx=4;if (idx <array5.count) {NSString *s = [array5 objectAtIndex:idx];NSLog(@"s = %@",s);}//--------------數組的遍曆//1.普通遍曆for (int i=0; i<array5.count; i++) {NSString *str = [array5 objectAtIndex:i];NSLog(@"%@",str);}//快速遍曆for (NSString *s in array5) {NSLog(@"%@",s);}//4.4之後的最佳化//1.建立一個素組NSArray *array7 = @[s1,s2,s3]; //等價於:NSArray *array7 = [NSArray arrayWithObjects:s1,s2,s3,nil];NSLog(@"array7 = %@",array7);NSString *str = array7[0];NSLog(@"array[7] = %@",str);//------------------可變數組//初始化,設定元素個數為5,可以更改的。(繼承自NSArray)NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:5];//想數組中添加一個元素[mutableArray addObject:@"aaa"];//向數組中指定下標插入元素[mutableArray insertObject:@"ccc" atIndex:0];NSLog(@"%@",mutableArray); //原來位置元素後移//移除最後一個元素[mutableArray removeLastObject];NSLog(@"移除最後元素後:%@",mutableArray);//移除指定元素[mutableArray removeObject:@"aaa"];//移除指定下標元素[mutableArray removeObjectAtIndex:0];//向數組中添加數組[mutableArray addObjectsFromArray:array1];//1.建立可變數組NSString *t1 = @"zhangsan ";NSString *t2 = @"lisi";NSString *t3 = @"wangwu ";// NSMutableArray *mArray1 = @[s1,s2,s3];//wrong.這裡建立了一個不可變數組NSMutableArray *mArray1 = [[NSMutableArray alloc] initWithObjects:s1,s2,s3, nil];//建立數組時,開闢3個空間用於儲存元素,儲存超過容量,數組自動增大空間NSMutableArray *mArray2 = [[NSMutableArray alloc] initWithCapacity:3];NSMutableArray *mArray3 = [NSMutableArray arrayWithCapacity:3];//2.添加元素[mArray2 addObject:t1];[mArray2 addObject:t2];[mArray2 addObject:t3];NSLog(@"mArray2= %@",mArray2);//將mArray2 中元素添加到mArray3中// [mArray3 addObjectsFromArray:mArray2];//將mArray2作為二維數字添加[mArray3 addObject:mArray2];NSLog(@"mArray3 = %@",mArray3);//3.插入元素[mArray2 insertObject:@"Jack" atIndex:0];NSLog(@"mArray2 = %@",mArray2);//4.替換元素[mArray2 replaceObjectAtIndex:0 withObject:@"John"];NSLog(@"替換:%@",mArray2);//5.互換兩個元素的位置[mArray2 exchangeObjectAtIndex:3 withObjectAtIndex:0];NSLog(@"mArray2 = %@",mArray2);//6.刪除元素//6.1根據下標刪除[mArray2 removeObjectAtIndex:2];NSLog(@"mArray2 = %@",mArray2);//6.2刪除最後一個元素[mArray2 removeLastObject];NSLog(@"mArray2 = %@",mArray2);//6.3刪除指定的對象//[mArray2 removeObject:@"zhangsan"];//6.4刪除所有元素[mArray2 removeAllObjects];NSLog(@"mArray2 = %@",mArray2);

以上所述是小編給大家介紹的iOS中NSArray數組常用處理方式,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.