天天看点

数组NSArray和可变数组NSMutableArray的基础知识数组 NSArray 可变数组 NSMutableArray 

数组 NSArray 

//创建
   NSMutableArray *firstMutableArray = [NSMutableArrayarray];
   NSMutableArray *secondMutableArray = [NSMutableArrayarrayWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",nil];
   NSMutableArray *fourMutableArray = [NSMutableArrayarrayWithArray:firstArray];
   NSMutableArray *sixMutableArray = [NSMutableArrayarrayWithCapacity:10];//创建一个容量为10的数组
   NSMutableArray *threeMutableArray = [NSMutableArrayarrayWithContentsOfURL:url];
   NSMutableArray *fiveMutableArray = [NSMutableArrayarrayWithContentsOfFile:path];
    
    
    //添加 add
    [firstMutableArrayaddObject:@"周六"];
    [firstMutableArrayaddObjectsFromArray:secondMutableArray];
    
    //插入 insert
    //将要插入的元素插入到当前的index位置上,原来在该位置上的元素自动往后移位
    [firstMutableArrayinsertObject:@"周日"atIndex:0];
    
    //删除 remove
    [firstMutableArrayremoveLastObject];       //删除数组内最后一个元素
    [firstMutableArrayremoveObject:@"周日"];    //删除数组内指定元素
    [firstMutableArrayremoveObjectAtIndex:0];  //删除数组内指定位置的元素
    [firstMutableArrayremoveObject:@"周一"inRange:NSMakeRange(1,2)];   //删指定范围内的某个元素
    [firstMutableArrayremoveAllObjects];       //删除数组内所有元素(清空)
    
    //替换 replace
    //用特定元素,替换数组内指定位置的元素
    [firstMutableArray replaceObjectAtIndex:0withObject:@"礼拜一"];
    //用另一个数组的指定范围,替换当前数组的指定范围
    [firstMutableArray replaceObjectsInRange:NSMakeRange(0,2)withObjectsFromArray:secondArrayrange:NSMakeRange(2,4)];
    
    //交换 指定位置的两个元素 exchang
    [firstMutableArray exchangeObjectAtIndex:0withObjectAtIndex:1];
    
    //遍历集合
   for (NSString *namein firstMutableArray) { //forin的collection仅可存放:数组,字典,集合
       NSLog(@"%@", name);
    }
           

可变数组 NSMutableArray 

//创建
   NSMutableArray *firstMutableArray = [NSMutableArrayarray];
   NSMutableArray *secondMutableArray = [NSMutableArrayarrayWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",nil];
   NSMutableArray *fourMutableArray = [NSMutableArrayarrayWithArray:firstArray];
   NSMutableArray *sixMutableArray = [NSMutableArrayarrayWithCapacity:10];//创建一个容量为10的数组
   NSMutableArray *threeMutableArray = [NSMutableArrayarrayWithContentsOfURL:url];
   NSMutableArray *fiveMutableArray = [NSMutableArrayarrayWithContentsOfFile:path];
    
    
    //添加 add
    [firstMutableArrayaddObject:@"周六"];
    [firstMutableArrayaddObjectsFromArray:secondMutableArray];
    
    //插入 insert
    //将要插入的元素插入到当前的index位置上,原来在该位置上的元素自动往后移位
    [firstMutableArrayinsertObject:@"周日"atIndex:0];
    
    //删除 remove
    [firstMutableArrayremoveLastObject];       //删除数组内最后一个元素
    [firstMutableArrayremoveObject:@"周日"];    //删除数组内指定元素
    [firstMutableArrayremoveObjectAtIndex:0];  //删除数组内指定位置的元素
    [firstMutableArrayremoveObject:@"周一"inRange:NSMakeRange(1,2)];   //删指定范围内的某个元素
    [firstMutableArrayremoveAllObjects];       //删除数组内所有元素(清空)
    
    //替换 replace
    //用特定元素,替换数组内指定位置的元素
    [firstMutableArray replaceObjectAtIndex:0withObject:@"礼拜一"];
    //用另一个数组的指定范围,替换当前数组的指定范围
    [firstMutableArray replaceObjectsInRange:NSMakeRange(0,2)withObjectsFromArray:secondArrayrange:NSMakeRange(2,4)];
    
    //交换 指定位置的两个元素 exchang
    [firstMutableArray exchangeObjectAtIndex:0withObjectAtIndex:1];
    
    //遍历集合
   for (NSString *namein firstMutableArray) { //forin的collection仅可存放:数组,字典,集合
       NSLog(@"%@", name);
    }