• 【Foundation Frame】NSMutableArray


    NSArray 代表集合元素不可变的集合,一旦NSArray被创建成功,程序不能向集合中添加新元素,删除旧元素,替换旧元素。
    NSMultableArray 恰恰相反,为可变元素的集合,可以增加、删除、替换元素。
    可进行的操作:
    1、创建 arrayWithCapacity
    2、增加元素,以add开头
    3、删除元素,以remove开头
    4、替换元素,以replace开头
    5、对集合本身排序,以sort开头(与NSArray不同,NSMutable是对元素集合自身进行排序,NSArray则是返回一个新的排序完成的元素集合。

    (注:以下程序仅供参考,还有很多方法没有列出来)
    //
    //  main.m
    //  NSMultableArray
    //
    //  Created by mac on 14-12-3.
    //  Copyright (c) 2014 mac. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    void multableCreatAray()
    {
       
    //创建MultableArray
       
    NSMutableArray * array1 =[NSMutableArray arrayWithCapacity:10];
       
    NSMutableArray * array2 =[NSMutableArray arrayWithObjects:@"Hello1",@"Hello2",@"Hello3",@"Hello4", nil];
       
    NSMutableArray * array3 =[NSMutableArray arrayWithObjects:@"Insert1",@"insert2",@"Insert3", nil];
       
    //array1中增加元素
        [array1
    addObject:@"Hello1"];
        [array1
    addObject:@"Hello2"];
        [array2
    addObject:@"Hello5"];
       
    NSLog(@"%@",array1);
       
    NSLog(@"%@",array2);
        [array1
    addObjectsFromArray:array2];
       
    NSLog(@"%@",array1);
        [array1
    insertObject:@"Insert1" atIndex:2];
       
    NSLog(@"%@",array1);
       
       
    //删除元素
        [array1
    removeObject:@"Hello1"];
       
    NSLog(@"%@",array1);
       
       
    NSMutableArray *array = [NSMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil];
       
    NSArray *newAdditions = [NSArray arrayWithObjects: @"a", @"b", nil];
       
    NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:1];
        [indexes
    addIndex:3];
        [array
    insertObjects:newAdditions atIndexes:indexes];
       
    NSLog(@"array: %@", array);
     
    }

    int main(int argc, const char * argv[]) {
       
    @autoreleasepool {
           
    // insert code here...
           
    NSLog(@"Hello, World!");
           
    multableCreatAray();
           
        }
       
    return 0;
    }

    ====>>>>>

    2014-12-03 17:32:27.148 NSMultableArray[2527:303] Hello, World!
    2014-12-03 17:32:27.151 NSMultableArray[2527:303] (
        Hello1,
        Hello2
    )
    2014-12-03 17:32:27.152 NSMultableArray[2527:303] (
        Hello1,
        Hello2,
        Hello3,
        Hello4,
        Hello5
    )
    2014-12-03 17:32:27.152 NSMultableArray[2527:303] (
        Hello1,
        Hello2,
        Hello1,
        Hello2,
        Hello3,
        Hello4,
        Hello5
    )
    2014-12-03 17:32:27.153 NSMultableArray[2527:303] (
        Hello1,
        Hello2,
        Insert1,
        Hello1,
        Hello2,
        Hello3,
        Hello4,
        Hello5
    )
    2014-12-03 17:32:27.153 NSMultableArray[2527:303] (
        Hello2,
        Insert1,
        Hello2,
        Hello3,
        Hello4,
        Hello5
    )
    2014-12-03 17:32:27.154 NSMultableArray[2527:303] array: (
        one,
        a,
        two,
        b,
        three,
        four
    )
    Program ended with exit code: 0
  • 相关阅读:
    LOJ.2721.[NOI2018]屠龙勇士(扩展CRT 扩展欧几里得)
    Codeforces.959E.Mahmoud and Ehab and the xor-MST(思路)
    BZOJ.3058.四叶草魔杖(Kruskal 状压DP)
    Codeforces.838E.Convex Countour(区间DP)
    Codeforces.838D.Airplane Arrangements(思路)
    Codeforces.997C.Sky Full of Stars(容斥 计数)
    Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)
    BZOJ.3759.Hungergame(博弈论 线性基)
    LOJ.2718.[NOI2018]归程(Kruskal重构树 倍增)
    序列化二叉树
  • 原文地址:https://www.cnblogs.com/shujucn/p/7481458.html
Copyright © 2020-2023  润新知