• oc - NSArray基础用法总结


    #import <Foundation/Foundation.h>


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

    @autoreleasepool {

    //类方法数组创建
    NSArray *array1 = [NSArray arrayWithObject:@"obj"];
    NSArray *array2 = [NSArray arrayWithObjects:@"obj1", @"obj2", @"obj3", nil];
    NSArray *array3 = [NSArray arrayWithArray:array2];
    NSLog(@"array1 :%@", array1);
    NSLog(@"array2 :%@", array2);
    NSLog(@"array3 :%@", array3);

    //实例方法创建数组
    NSArray *array4 = [[NSArray alloc] initWithObjects:@"AAA", @"bbb", nil];
    NSLog(@"array4 :%@", array4);

    //数组个数
    NSLog(@"array3 count :%d", [array3 count]);

    //访问元素
    NSLog(@"obj at index :%@", [array2 objectAtIndex:2]);

    //追加(返回新的array对象)
    NSArray *array5 = [array3 arrayByAddingObject:@"ccc"];
    NSLog(@"array5 :%@", array5);


    //根据指定的字符串连接数组元素
    NSString *joinString1 = [array5 componentsJoinedByString:@"|"];
    NSLog(@"joinString :%@",joinString1);


    //是否包含指定对象
    NSLog(@"isContains :%d", [array5 containsObject:@"obj2"]);


    //查找某个对象所在索引
    NSLog(@"indexOfObject :%d",[array5 indexOfObject:@"obj3"]);

    //最后一个元素
    NSLog(@"lastObejct :%@", [array5 lastObject]);

    //遍历数组
    for (id element in array5) {
    NSLog(@"element :%@", element);
    }




    //可变数组(期望容量设置为3)
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:3];
    //直接添加
    [mutableArray addObject:@"aaa"];
    [mutableArray addObject:@"eee"];
    [mutableArray addObject:@"000"];
    NSLog(@"addObject :%@", mutableArray);

    //插入元素
    [mutableArray insertObject:@"ccc" atIndex:0];
    NSLog(@"insertObject :%@", mutableArray);

    //移除指定元素
    [mutableArray removeObject:@"ccc"];
    NSLog(@"removeObject :%@", mutableArray);

    //移除指定下标元素
    [mutableArray removeObjectAtIndex:0];
    NSLog(@"removeObjectAtIndex :%@", mutableArray);

    //移除最后一个元素
    [mutableArray removeLastObject];
    NSLog(@"removeLastObject :%@", mutableArray);

    //添加数组
    [mutableArray addObjectsFromArray:array5];
    NSLog(@"addObjectsFromArray :%@", mutableArray);


    //移除指定数组中的内容
    [mutableArray removeObjectsInArray:array2];
    NSLog(@"removeObjectsInArray :%@", mutableArray);


    //指定索引替换对象
    [mutableArray replaceObjectAtIndex:0 withObject:@"==="];
    NSLog(@"replaceObjectAtIndex :%@", mutableArray);




    //删除全部对象
    [mutableArray removeAllObjects];
    NSLog(@"removeAllObjects :%@", mutableArray);
    }
    return 0;
    }

  • 相关阅读:
    [USACO08JAN]牛大赛Cow Contest
    [USACO07NOV]牛栏Cow Hurdles
    [USACO09JAN]最好的地方Best Spot
    [USACO08OPEN]寻宝之路Clear And Present Danger
    [BZOJ 1146] [CTSC2008]网络管理Network(树状数组+主席树)
    bzoj4009: [HNOI2015]接水果(整体二分)
    【bzoj2141】排队 [国家集训队2011]排队(树套树)
    洛谷P4689 [Ynoi2016]这是我自己的发明(树上莫队+树链剖分)
    【bzoj3083】遥远的国度(树链剖分+线段树)
    CF311B Cats Transport(斜率优化)
  • 原文地址:https://www.cnblogs.com/immensity1993/p/5302588.html
Copyright © 2020-2023  润新知