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)創建一個數組,此數組中的元素來自array1 NSArray *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.lastObject NSLog(@"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數組常用處理方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!