Objective-C:NSMutableArray類的常見操作

來源:互聯網
上載者:User

標籤:

可變數組NSMutableArray的內容大小是可變的,因此它的常見操作無非增刪該查,

具體一些就是:建立、添加、刪除、替換、插入、清空等等。。

 

//

//  main.m

//  02-NSMutableArray

//

//  Created by ma c on 15/8/18.

//  Copyright (c) 2015年 bjsxt. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

int main(int argc, const char * argv[])

{

    @autoreleasepool

    {

        //NSMutableArray可變數組

        //1.建立

        //1.1通過不可變數組建立可變數組

        NSArray *array = @[@1,@2,@3,@4,@5];

        NSMutableArray *mutablearray1 = [NSMutableArray arrayWithArray:array];

        //1.2建立空的可變數組

        NSMutableArray *mutablearray2 = [NSMutableArray array];

        //1.3 建立固定大小的數組

        NSMutableArray *mutablearray3 = [NSMutableArray arrayWithCapacity:10];

        

        //2.添加對象

        [mutablearray1 addObject:@6];

        [mutablearray1 addObject:@3];

        NSLog(@"%@",mutablearray1);

        

        //3.刪除對象

        [mutablearray1 removeObject:@3];

        NSLog(@"%@",mutablearray1);

        //[mutablearray1 removeLastObject];//刪除最後一個

        //[mutablearray1 removeObjectAtIndex:2];//按索引刪除

        //NSLog(@"%@",mutablearray1);

        

        

        //4.替換對象

        [mutablearray1 replaceObjectAtIndex:2 withObject:@10];

        NSLog(@"%@",mutablearray1);

        

        //4.插入對象

        [mutablearray1 insertObject:@3 atIndex:2];

        NSLog(@"%@",mutablearray1);

        

        //5.清空所有對象

        [mutablearray1 removeAllObjects];

        NSLog(@"%@",mutablearray1);

        

    }

    return 0;

}

 

Objective-C:NSMutableArray類的常見操作

聯繫我們

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