NSMutableArray--可變數組,nsmutablearray--

來源:互聯網
上載者:User

NSMutableArray--可變數組,nsmutablearray--

////  main.m//  OC04-task-08////  Created by keyzhang on 15-1-24.//  Copyright (c) 2015年 keyzhang. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        // insert code here...                /////////////NSMutableArray--可變數組///////////                //使用了父類的構造方法構造一個可變數組        NSMutableArray *arr = [NSMutableArray array];                //NSMutableArray自己的構造方法,表示初始化數組的容量大小,注意:這裡只是為了代碼的可讀性        NSMutableArray *arr0 = [NSMutableArray arrayWithCapacity:2];        //添加元素(對象)        /*         Inserts a given object at the end of the array.         */        [arr0 addObject:@"1"];        [arr0 addObject:@"2"];        [arr0 addObject:@"3"];        NSLog(@"arr0 is %@",arr0);        //用父類的方法構造        NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"1",@"3", nil];        NSLog(@"arr1 is %@",arr1);                //向數組的末尾添加一個新元素        [arr addObject:@"x"];        NSLog(@"arr is %@", arr);                //插入元素        [arr1 insertObject:@"2" atIndex:1];        [arr1 insertObject:@"0" atIndex:0];        NSLog(@"arr1 is %@",arr1);                //刪除元素        //刪除最後一個元素        [arr1 removeLastObject];        NSLog(@"arr1 is %@",arr1);                //刪除指定下標元素        [arr1 removeObjectAtIndex:1];        NSLog(@"arr1 is %@",arr1);                //刪除指定元素 //如果元素不存在,則什麼也不做        [arr1 removeObject:@"2"];        NSLog(@"arr1 is %@",arr1);        //移除所有元素        [arr1 removeAllObjects];        NSLog(@"arr1 is %@",arr1);                        //添加多個元素        NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"1",@"2", nil];        NSArray *lists = @[@"3",@"4",@"5",@"6"];        [arr2 addObjectsFromArray:lists];        NSLog(@"arr2 is %@",arr2);                //替換和交換元素        //根據傳進來的對象替換下標的目標對象        [arr2 replaceObjectAtIndex:2 withObject:@"7"];        NSLog(@"arr2 is %@",arr2);        //根據兩個元素的下標交換元素        [arr2 exchangeObjectAtIndex:2 withObjectAtIndex:5];        NSLog(@"arr2 is %@",arr2);                //擴充:        Person *ps = [[Person alloc] init];        ps.name = @"tom";        ps.age = 10;                //數組的遍曆        NSArray *arr3 = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",ps];        //傳統遍曆法        for (int i = 0; i < [arr3 count]; i++) {            NSLog(@"arr3[%d] is %@",i, arr3[i]);        }                //快速遍曆法        for (id value in arr3) {            //迴圈塊            if ([value isMemberOfClass:[Person class]]) {                Person *ps = (Person *)value;                NSLog(@"person name is %@",ps.name);                continue;            }            NSLog(@"value is %@",value);                    }                        //mutableCopy        NSArray *arr4 = @[@"1", @"2", @"3"];        NSLog(@"arr4 is %@", arr4);        NSMutableArray *arr5 = [arr4 mutableCopy];        [arr5 addObject:@"4"];        NSLog(@"arr5 is %@", arr5);                //        NSMutableArray *arr6 = [NSMutableArray arrayWithArray:arr4];                    }    return 0;}

聯繫我們

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