Objective-c的Foundation中常用類3——數組NSArray、NSMutableArray

來源:互聯網
上載者:User

oc中的數組和我們以前學過的c、c++或者是java中的數組還是有很大的區別的

Foundation中的數組是一組有序的對象的集合,數組中不可以存放基礎資料型別 (Elementary Data Type),只能存放類的執行個體(對象),如果需要將基礎資料型別 (Elementary Data Type)、結構體存放在數組中,需要先進行封裝

NSArray 不可變數組

單一元素初始化

//初始化一個單元素數組        NSArray *array = [NSArray arrayWithObject:@"one"];                NSLog(@"%@",array);                //初始化一個多元素數組,注意和上面的對比多了一個s        NSArray *array2 = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];                NSLog(@"%@",array2);                //初始化一個多元素數組        NSArray *array3 = [[NSArray alloc]initWithObjects:@"you",@"me",@"he",nil];        NSLog(@"%@",array3);                //複製一個數組        NSArray *array4 = [[NSArray alloc]initWithArray:array3];        NSLog(@"%@",array4);                        //擷取數組中的元素的個數        NSInteger *count = [array4 count];        NSLog(@"%d",count);//列印   3                //根據下標訪問對象        NSString *firstObj = [array4 objectAtIndex:2];        NSLog(@"%@",firstObj);//列印  he

//在原數組的基礎上追加對象,並產生一個新的數組

        NSArray *array5 = [array4arrayByAddingObject:@"it"];

        NSLog(@"%@",array5);

//用指定的字串把數組的元素串連

        NSString *arrayString = [array5componentsJoinedByString:@"/"];

        NSLog(@"%@",arrayString);//列印  you/me/he/it

        

        //數組中是否包含某對象

        BOOL isContain = [array5
containsObject:@"it"];

        NSLog(@"%d",isContain);//列印   1

        

        //查詢對象在數組中的位置

        NSInteger *atIndex1 = [array5
indexOfObject:@"it"];

        NSInteger *atIndex2 = [array5
indexOfObject:@"yy"];

        NSLog(@"at   %d",atIndex1);//列印  at   3

        NSLog(@"at   %d",atIndex2);//不存在列印

2、可變數組 NSMutableArray

繼承自NSArray,因此可以使用NSArray的所有方法

//向數組中添加一個元素        NSMutableArray *array1 = [NSMutableArray arrayWithObject:@"hello"];        [array1 addObject:@"world"];        NSLog(@"%@",array1);                        //在數組的指定的下標處添加元素        [array1 insertObject:@"nihao" atIndex:0];        NSLog(@"%@",array1);                        //按下標刪除元素        [array1 removeObjectAtIndex:0];        NSLog(@"%@",array1);                        //刪除最後一個元素        [array1 removeLastObject];        NSLog(@"%@",array1);                //向數組中添加數組        [array1 addObjectsFromArray:array1];        NSLog(@"%@",array1);                        //替換指定位置元素        [array1 replaceObjectAtIndex:1 withObject:@"world"];        NSLog(@"%@",array1);

遍曆數組

NSArray *array2 = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five", nil];        for(int i=0;i<[array2 count];i++)        {            NSString *element = [array2 objectAtIndex:i];            NSLog(@"%@",element);        }        //方法二,枚舉法        for(NSString *element in array2)        {            NSLog(@"%@",element);        }        

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.