数组

来源:互联网 发布:java性能测试代码 编辑:程序博客网 时间:2024/05/22 08:11
不可变的数组// 1.初始化一个数组 此方法中最后一个元素必须是nil A comma-separated list of objects ending with nil.        // 用逗号分隔的一序列对象,nil是最后一个元素        NSArray *array = [[NSArray alloc]initWithObjects:@"one",@"two",@"three", nil];        NSLog(@"%@",array); // 2.新语法        NSArray *array1 = @[@"one",@"two",@"three"];        NSLog(@"%@",array1);    // int float char double bool// 3.分装基本数据类型// 数组中可以存储不同类型的对象        int i = 1;        NSNumber *number = [[NSNumber alloc]initWithInt:i];        NSArray *array2 = @[@"one",@"two",number];        NSLog(@"array2 = %@",array2);// 4 数组中实际是存储不同类型的对象的地址 数组中也可以存储地址        NSArray *array3 = @[@"one",@"two",@"three"];        NSArray *array4 = @[@"1",@"2",@"3"];        NSArray *newArray = @[array3,array4];        NSLog(@"new Array = %@",newArray);// 5.获得数组元素个数        NSUInteger count = [newArray count];        NSLog(@"count = %lu",count);  // 6.判断数组中是否包含某元素        BOOL isContain = [newArray containsObject:array3];//        Returns a Boolean value that indicates whether a given object is present in the array.// 返回一个BOOL值来验证是否一个对象在这个数组中//        This method determines whether anObject is present in the array by sending an isEqual: message to each of the array’s objects (and passing anObject as the parameter to each isEqual: message).        // 这个方法决定了 是否这个对象存在于数组中 通过发送一个isEqual:消息给数组中的每个元素        if (isContain) {            NSLog(@"%d--包含",isContain);        } else {            NSLog(@"%d--不包含",isContain);        }     // 1.使用for循环来遍历数组        NSArray *array = @[@"one",@"two",@"three"];        for (int i = 0 ; i < array.count; i++) {            NSString *string = array[i];            NSLog(@"%@",string);        }        // 2.增强for循环来遍历数组//        for (NSString *string1 in array) {        for (id obj in array) {            NSLog(@"%@",obj);        }         // 数组排序        NSArray *array = @[@"a",@"c",@"b",@"d"];        NSLog(@"%@",array);//        BOOL yes = 3;//        NSLog(@"%d",yes);//        //        NSComparisonResult result = [@"a" compare:@"b"];//        NSLog(@"result = %ld",result);//        SEL sel = @selector(compare:);        // 排序//        NSArray *newArray = [array sortedArrayUsingSelector:sel];//        NSLog(@"%@",newArray);        //        void (^block)(int,int);  尖括号里面是block的名字 后面是参数        NSArray *newArray1 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {//            return [obj1 compare:obj2];            if ([obj1 isGreaterThan:obj2]) { // 大于                return NSOrderedDescending;            } else  if ([obj1 isEqual:obj2]){ // 相等                return NSOrderedAscending;            } else { // 小于                return NSOrderedSame;            }        }];        NSLog(@"newArray1 = %@",newArray1);可变的数组        NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3", nil];        NSArray *array = @[@"4",@"5",@"6"];                // 1.用不可变的数组初始化一个可变的数组        NSMutableArray *mutableArray1 = [[NSMutableArray alloc]initWithArray:array];        NSLog(@"%@",mutableArray1);                NSString *string = @"Hello World";        NSString *string1 = @"Hello World";//        // 2.指定对象插入的位置when you create an array, the specified size is regarded as a “hint”        [mutableArray insertObject:string atIndex:3];        NSLog(@"%@",mutableArray);                // 3.删除会通过对象地址删除数组中所有的同一个地址的对象        // 往数组中添加一个对象        [mutableArray addObject:string1];//        Inserts a given object at the end of the array.        // 在数组的最后插入给定的元素        NSLog(@"%@",mutableArray);        // 把同一个地址的对象全部删除                // 4.删除相同内容的元素        [mutableArray removeObject:string]; // 连同string1一起删除 //        isEqual:        NSLog(@"%@",mutableArray);                // 5.内存地址        NSMutableArray *mutableArrayTest = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3", nil];//        NSMutableArray *mutableArrayTest1 = [[NSMutableArray alloc]initWithArray:mutableArrayTest];        NSMutableArray *mutableArrayTest2 = [NSMutableArray arrayWithArray:mutableArrayTest];        NSLog(@"pppp --- %p %p",mutableArrayTest,mutableArrayTest2);                        NSArray *arrayTest = [[NSArray alloc]initWithObjects:@"1",@"2" ,nil];        NSArray *arrayTest2 = [NSArray arrayWithArray:arrayTest];        NSArray *arrayTest3 = [[NSArray alloc]initWithArray:arrayTest];        NSLog(@"ppp-----%p %p %p",arrayTest,arrayTest2,arrayTest3);        //        Creates and returns an array containing the objects in another given array.                       //        - (void)addObject:(id)anObject;//        - (void)insertObject:(id)anObject atIndex:(NSUInteger)index;//        - (void)removeLastObject;//        - (void)removeObjectAtIndex:(NSUInteger)index;//        - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;        

0 0