数组和可变数组的基本用法 


NSString *st1 = @"158";

        NSString *str2 = @"39";

        int a = [st1 intValue];

        int b = [str2 intValue];

        

        

        int result = a - b;

        

        NSString *str = [NSString stringWithFormat:@"%d", result];

        NSLog(@"str is %@", str);

        

        

        NSString *string = @"123-456-789-000";

        

        //方法一

//        NSString *sub1 = [string substringWithRange:NSMakeRange(0, 3)];

//        NSString *sub2 = [string substringWithRange:NSMakeRange(4, 3)];

//        NSString *sub3 = [string substringWithRange:NSMakeRange(8, 3)];

//        NSString *sub4 = [string substringWithRange:NSMakeRange(12, 3)];

//        NSString *resultStr = [NSString stringWithFormat:@"%@%@%@%@", sub1, sub2, sub3, sub4];

//        NSLog(@"resultStr is %@", resultStr);

        

        //方法二

//        NSString *resultStr = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];

//        NSLog(@"resultStr is %@", resultStr);

        

        

        NSArray *array =  [string  componentsSeparatedByString:@"-"];

        NSString *newString = [array componentsJoinedByString:@""];

        NSLog(@"newString-------------------%@",newString);

        

        //方法三

        NSArray *arr = [string componentsSeparatedByString:@"-"];

        NSString *resultStr = [arr componentsJoinedByString:@""];

        NSLog(@"resultStr is %@", resultStr);


        

    }

    return 0;

}

 /***************NSMutableArray***************/

        

        //使用父类的构造方法创建一个可变数组

//        NSMutableArray *arr = [NSMutableArray array];

        

        //NSMutableArray自己的构造方法,initWithCapacity表示初始化数组的容量大小,注意:不能限制容量大小,只是提高代码的可读性

        NSMutableArray *arr0 = [[NSMutableArray alloc] initWithCapacity:5];

        

        NSMutableArray *ar0 = [NSMutableArray array];

        [ar0 addObject:@"3234224142424"];

        NSLog(@"ar0-----------%@",ar0);

        

        NSMutableArray *ar1 = [NSMutableArray arrayWithArray:ar0];

        NSLog(@"ar1-----------%@",ar1);

        

        NSMutableArray *ar2 = [NSMutableArray arrayWithObjects:ar0,ar1, nil];

        NSLog(@"ar2-----------%@",ar2);


        

        //添加元素(对象)

        [arr0 addObject:@"1"];

        [arr0 addObject:@"2"];

        [arr0 addObject:@"3"];

        [arr0 addObject:@"4"];

        

        NSLog(@"arr0 is %@", arr0);

        

        

        //用父类的构造器

        NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"1", @"3", nil];

        NSLog(@"arr1 is %@", arr1);

        

        //插入元素

        [arr1 insertObject:@"2" atIndex:1];

        [arr1 insertObject:@"0" atIndex:0];

        NSLog(@"arr1 is %@", arr1);

        

        //删除元素

        [arr1 removeLastObject];

        NSLog(@"arr1 is %@", arr1);

        

        //删除指定下标元素

        [arr1 removeObjectAtIndex:0];

        NSLog(@"arr1 is %@", arr1);

        

        //删除指定元素//如果元素不在数组中,则什么也不做

        [arr1 removeObject:@"3"];

        NSLog(@"arr1 is %@", arr1);

        

        //删除所有元素

        [arr1 removeAllObjects];

        NSLog(@"arr1 is %@", arr1);

        

        

        //添加多个元素

        NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];

        NSArray *lists = @[@"4", @"5", @"6"];

        [arr2 addObjectsFromArray:lists];

        NSLog(@"arr2 is %@", arr2);

        

        //替换和交换元素

        //根据传过来的对象替换下标对象

        [arr2 replaceObjectAtIndex:arr2.count - 1 withObject:@"7"];

        NSLog(@"arr2 is %@", arr2);

        

        //根据两个元素的下标交换元素

        [arr2 exchangeObjectAtIndex:0 withObjectAtIndex:arr2.count - 1];

        NSLog(@"arr2 is %@", arr2);


        //数组的遍历

//        for (int i = 0; i < arr2.count; i++) {

//            NSString *obj = [arr2 objectAtIndex:i];

//            NSLog(@"arr2[%d] is %@", i, obj);

//        }

        

        //快速便利

        for (NSString *obj in arr2) {

            //循环块

            NSLog(@"arr2[%ld] is %@", [arr2 indexOfObject:obj], obj);

        }