zoukankan      html  css  js  c++  java
  • NSArray和NSMutableArray的详解

    数组中不能存放基本数据类型,必须存放对象,因此如果要存放基本数据类型,先进行NSTimer封装

    NSArray的用法:

    第一、初始化

    NSArray *firstArray=[[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
            NSArray *secondArray=[NSArray arrayWithArray:firstArray];
    
    

    第二、获取元素个数和访问

            NSLog(@"the number is %ld",[secondArray count]);
            NSLog(@"the value is %@",[secondArray objectAtIndex:2]);
    


    第三、追加数据元素

            NSArray *thirdArray=[firstArray arrayByAddingObjectsFromArray:secondArray];
    


    第四、数组转化为字符串

     

            NSString *str=[firstArray componentsJoinedByString:@".."];
            NSLog(@"the number is %@",str);
    


    第五、判断是否包含字符串

     

            NSArray *firstArray=[[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
            NSLog(@"has value %d",[firstArray containsObject:@"two"]);
            NSLog(@"has value %ld",[firstArray indexOfObject:@"two"]);
            NSLog(@"the last object is %@",[firstArray lastObject]);
    


    NSMutalbeArray 的用法-

    第一、基本的增删改

     

         NSMutableArray *mutableArr=[NSMutableArray arrayWithCapacity:4];
            [mutableArr addObject:@"hello"];
            [mutableArr addObject:@"hello"];
            [mutableArr addObject:@"hello"];
    
            [mutableArr addObject:@"richard"];
            [mutableArr insertObject:@"yang" atIndex:1];
            NSLog(@"%@",mutableArr);
            [mutableArr removeObject:@"hello"];
            [mutableArr removeObjectAtIndex:0];
            [mutableArr removeLastObject];
            NSLog(@"%@",mutableArr);


    第二、替换操作

     

            [mutableArr replaceObjectAtIndex:0 withObject:@"kaixin"];
    


    第三、遍历

     

            NSMutableArray *mutableArr=[NSMutableArray arrayWithCapacity:4];
            [mutableArr addObject:@"hello"];
            [mutableArr addObject:@"hello"];
            [mutableArr addObject:@"hello"];
            for(int index=0;index<[mutableArr count];index++)
            {
                NSLog(@"the val is %@",[mutableArr objectAtIndex:index]);
            }
            for(NSString *str in mutableArr)
            {
                NSLog(@"%@",str);
            }
            for (id str in mutableArr) {
                NSLog(@"%@",str);
            }
    



  • 相关阅读:
    Java-Maven(三):Maven坐标、Maven仓库、Maven生命周期
    Java-Maven(二):Maven常用命令
    CF796C Bank Hacking题解
    米特运输——(dfs)
    lower_bound()和upper_bound()如果搜下标到头返回啥
    骑士https://vjudge.net/contest/364177#problem/Y——(基环树)
    CF1215DTicketGame——(博弈)
    #define xhxj (Xin Hang senior sister(学姐))
    The Meaningless Game 题解
    P2827 蚯蚓
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3364686.html
Copyright © 2011-2022 走看看