程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> NSMutableArray

NSMutableArray

編輯:關於C++
//
//  main.m
//  OC04-task-08
//
//  Created by keyzhang on 15-1-24.
//  Copyright (c) 2015年 keyzhang. All rights reserved.
//

#import 
#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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved