zoukankan      html  css  js  c++  java
  • NSMutableArray 初始化与添加删除程序

           Person *person1=[[Person alloc]initWithName:@"Kenshin"];
           Person *person2=[[Person alloc]initWithName:@"Kaoru"];
           Person *person3=[[Person alloc]initWithName:@"Rosa"];
           NSMutableArray *array1=[NSMutableArray arrayWithObjects:
                                       person1,
                                       person2,
                                       person3,
                                       nil
                                   ];
           //NSLog(@"%@",array1);
           /*结果:
            (
            "name=Kenshin",
            "name=Kaoru",
            "name=Rosa" )
            */
           
           Person *person4=[[Person alloc]initWithName:@"Jack"];//此时person4的retainCount为1
           [array1 addObject:person4];//添加一个元素,此时person4的retainCount为2
           //NSLog(@"%@",array1);
           /*结果:
            (
            "name=Kenshin",
            "name=Kaoru", "name=Rosa", "name=Jack"
            ) */
           
           [array1 removeObject:person3];//删除一个元素 NSLog(@"%@",array1);
           /*结果:
            (
            "name=Kenshin",
            "name=Kaoru",
            "name=Jack" )
            */
           //NSLog(@"%@",array1);
           
           [array1 removeLastObject];//删除最后一个元素,//此时person4的retainCount为1 NSLog(@"%@",array1);
           /*结果:
            (
            "name=Kenshin",
            "name=Kaoru" )
            */
           NSLog(@"%@",array1);
           
           [array1 removeAllObjects];//删除所以元素
           NSLog(@"%@",array1);
     
  • 相关阅读:
    第2课 有符号与无符号
    第1课 基本数据类型
    HDU 5821 Ball
    Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation
    HDU 5810 Balls and Boxes
    HDU 5818 Joint Stacks
    HDU 5813 Elegant Construction
    Codeforces Round #357 (Div. 2)C. Heap Operations
    Codeforces Round #364 (Div. 2) C. They Are Everywhere
    HDU5806 NanoApe Loves Sequence Ⅱ
  • 原文地址:https://www.cnblogs.com/kluan/p/4819423.html
Copyright © 2011-2022 走看看