天天看點

數組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);
    }