OC4_可变数组

//

//  main.m

//  OC4_可变数组

//

//  Created by zhangxueming on 15/6/11.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

//NSMutableArray



int main(int argc, const char * argv[]) {

    @autoreleasepool {

        //创建一个指定容量大小的可变数组对象

        NSMutableArray *mulArray = [[NSMutableArray alloc] initWithCapacity:20];

        NSMutableArray *mulArray2 = [NSMutableArray arrayWithCapacity:20];

        NSLog(@"mulArray = %@ mulArray2 = %@", mulArray, mulArray2);

        //添加数组元素

        [mulArray addObject:@"one"];

        NSLog(@"mulArray = %@", mulArray);

        //在指定位置添加数组元素

        [mulArray insertObject:@"two" atIndex:0];

        NSLog(@"mulArray = %@", mulArray);

        //将传入的数组添加到可变数组中

        [mulArray addObjectsFromArray:@[@"three",@"four",@"five",@"six"]];

        NSLog(@"mulArray = %@", mulArray);

        

        //删除数组中最后一个元素

        [mulArray removeLastObject];

        NSLog(@"mulArray = %@", mulArray);

        //删除指定位置的元素

        [mulArray removeObjectAtIndex:1];

        NSLog(@"mulArray = %@", mulArray);

        //替换指定位置的数组元素

        [mulArray replaceObjectAtIndex:2 withObject:@"helloworld"];

        NSLog(@"mulArray = %@", mulArray);

        //交换指定位置的数组元素

        [mulArray exchangeObjectAtIndex:0 withObjectAtIndex:2];

        NSLog(@"mulArray = %@", mulArray);

        //删除数组中所有的元素

        [mulArray removeAllObjects];

        NSLog(@"mulArray = %@", mulArray);

        

        NSMutableArray *mulArray3 = [NSMutableArray arrayWithObjects:@"one",@"two",@"two",@"three",@"two",@"three",@"four",@"five", nil];

        //删除指定范围内的指定的元素

        [mulArray3 removeObject:@"three" inRange:NSMakeRange(2, 4)];

        NSLog(@"mulArray3 = %@", mulArray3);

        

        //删除数组中所有出现的目标元素

        [mulArray3 removeObject:@"two"];

        NSLog(@"mulArray3 = %@", mulArray3);

        

        //删除所有在传入数组中出现的元素

        [mulArray3 removeObjectsInArray:@[@"one",@"four",@"six"]];

         NSLog(@"mulArray3 = %@", mulArray3);

        

        //删除指定范围的数组元素

        NSMutableArray *mulArray4 = [NSMutableArray arrayWithArray:@[@"one",@"two",@"three",@"four"]];

        [mulArray4 removeObjectsInRange:NSMakeRange(1, 2)];

        NSLog(@"mulArray4 = %@", mulArray4);

        

        //修改(重置)数组

        [mulArray4 setArray:@[@"hello",@"world",@"qian",@"feng"]];

        NSLog(@"mulArray4 = %@", mulArray4);

        

        //数组排序

        NSMutableArray *sortArray = [NSMutableArray arrayWithArray:@[@"one",@"two",@"three",@"four",@"five"]];

        [sortArray sortUsingSelector:@selector(compare:)];

        NSLog(@"sortArray = %@", sortArray);

        

        //用传入的数组替换指定范围内的数组元素

        [mulArray4 replaceObjectsInRange:NSMakeRange(0, 2) withObjectsFromArray:@[@"one",@"two",@"three"]];

        NSLog(@"mulArray4 = %@", mulArray4);

        

        //替换指定范围内的数组元素

//- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;

        NSMutableArray *mulArray5= [NSMutableArray arrayWithArray:@[@"one",@"two",@"three",@"four",@"five"]];

        NSArray *array = @[@"hello",@"world",@"qian",@"feng",@"jiaoxue"];

        [mulArray5 replaceObjectsInRange:NSMakeRange(1, 3) withObjectsFromArray:array range:NSMakeRange(0, 2)];

        NSLog(@"mulArray5 = %@", mulArray5);

        

//        - (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;

        NSMutableIndexSet *indexset = [NSMutableIndexSet indexSetWithIndex:1];

        [indexset addIndex:3];

        [mulArray5 insertObjects:@[@"ten",@"nine"] atIndexes:indexset];

        NSLog(@"mulArray5 = %@", mulArray5);

        

        //删除下表位置的元素

//        - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;

        //用传入数组替换指定位置的数组元素

//        - (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;

        [mulArray5 replaceObjectsAtIndexes:indexset withObjects:@[@"qian",@"feng"]];

        NSLog(@"mulArray5 = %@", mulArray5);

    }

    return 0;

}

 

你可能感兴趣的:(可变数组)