zoukankan      html  css  js  c++  java
  • [OC Foundation框架

    可变的NSArray,可以随意添加OC对象

     
    1.创建
     1 void arrayCreate()
     2 {
     3     NSMutableArray *array = [NSMutableArray arrayWithObject:@"1"];
     4    
     5     [array addObject:@"2"];
     6     [array addObject:@"3"];
     7    
     8     [array removeObject:@"2"];
     9     NSLog(@"%@", array);
    10 }
     
    NSMutableArray 不能使用@[]创建
     
     
    2.内存管理
    当Array release的时候,里面的元素也会release一次
     1 void memoryManage()
     2 {
     3     NSMutableArray *array = [NSMutableArray array];
     4     Student *stu1 = [Student initWithAge:12];
     5     Student *stu2 = [Student initWithAge:42];
     6    
     7     //Will retain stu1 one time automatically
     8     [array addObject:stu1];
     9     [array addObject:stu2];
    10    
    11     NSLog(@"add--> stu1: %zi", [stu1 retainCount]);
    12    
    13     //Will release stu1 one time automatically
    14     [array removeObject:stu1];
    15     NSLog(@"remoe--> stu1: %zi", [stu1 retainCount]);
    16    
    17     NSLog(@"%@", array);
    18    
    19     //All element will be released one time
    20     [array release];
    21 }
     
    3.替换元素
    1 void replaceArray()
    2 {
    3     NSMutableArray  *array = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];
    4     [array replaceObjectAtIndex:2 withObject:@"a"];
    5     NSLog(@"%@", array);
    6 }
     
    4.排序
    1 void arraySort()
    2 {
    3     NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1", @"3", @"3", nil];
    4     [array sortedArrayUsingSelector:@selector(compare:)];
    5     NSLog(@"%@", array);
    6 }
     
    5.删除元素
    1         NSMutableArray *a = [NSMutableArray array];
    2         [a addObject:@1];
    3         [a addObject:@2];
    4         [a removeObject:@1];
    5         NSLog(@"%@", a);
    6         [a removeAllObjects];
    7         NSLog(@"%@", a);
     
     
  • 相关阅读:
    关于带权并查集
    Connections in Galaxy War ZOJ
    Supermarket POJ
    并查集判树 poj 1308
    最长异或值路径【字典树应用】
    最大异或对
    trie字典树【模板题】
    UVA536 二叉树重建 Tree Recovery
    UVA1584环状序列 Circular Sequence
    【CodeForces 622F】The Sum of the k-th Powers
  • 原文地址:https://www.cnblogs.com/hellovoidworld/p/4119408.html
Copyright © 2011-2022 走看看