OC语言数组

    // Student.h文件
@property(nonatomic, copy)NSString *name;
- (id)initWithName:(NSString *)name;
    // Book.h文件
@property(nonatomic, copy)NSString *name;
@property(nonatomic, assign)CGFloat price;
- (id)initWithName:(NSString *)name
             price:(CGFloat)price;
+ (Book *)bookWithName:(NSString *)name
                 price:(CGFloat)price;
    // OC的数组,里边存放的一定是对象
    // 不可变数组
    NSArray *arr = [[NSArray alloc] init];
    // 用便利构造器创建一个空数组
    NSArray *arr1 = [NSArray arrayWithArray:0];
    // 无参数的时候可以只保留array
    // 字面量
    NSArray *arr2 = @[@"1", @"2", @"3", @"4", @"5"];
    // count:数组里的元素个数
    NSLog(@"%ld", arr2.count);
    // 也是通过下标进行取值,返回一个对象
    NSLog(@"%@", [arr2 objectAtIndex:2]);
    NSLog(@"%@", arr2);
    // for对数组进行遍历
    for (NSInteger i = 0; i < arr2.count; i++) {
        NSLog(@"%@", arr2[i]);
    }
    // 判断当前对象@"6",是否在我们数组的里边
    NSLog(@"%d", [arr2 containsObject:@"6"]);
    Student *stu1 = [[Student alloc] initWithName:@"商帅"];
    Student *stu2 = [[Student alloc] initWithName:@"刘山山"];
    Student *stu3 = [[Student alloc] initWithName:@"杨林"];
    Student *stu4 = [[Student alloc] initWithName:@"腾飞"];
    Student *stu5 = [[Student alloc] initWithName:@"谭泉元"];
//    NSArray *stu0 = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, stu4, stu5, nil];
//    NSArray *stu0 = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, stu5, nil];
    NSArray *stu = @[stu1.name, stu2.name, stu3.name, stu4.name, stu5.name];
    for (NSInteger i = 0; i < stu.count; i++) {
        NSLog(@"%@", stu[i]);
    }
    NSLog(@"*********");
    // 快速枚举:能快速的遍历数组等容器(collection)对象
    // 都是对容器里的每一个元素的遍历
    // 为了增加代码的可读性,避免不必要的错误,尽量让forin里前部分的类型和数组里元素类型相同
    NSArray *arr3 = @[@"薛楠",@"杨林",@"刘山山",@"商帅"];
    for (NSString *str in arr3) {    // 换成NSArray *也可以接收,打印出元素(本身是字符串)
        NSLog(@"%@", str);
    }
    NSLog(@"*********");
    NSArray *arr4 = @[@"周圣民", @"腾飞", @"刘鑫奇", @"大表姐"];
    NSArray *arr0 = @[arr3, arr4];
    for (NSArray *temp in arr0) {
        for (NSString *str0 in temp) {
            NSLog(@"%@", str0);
        }
    }
    NSArray *arr5 = @[stu1, stu2, stu3];
    NSArray *arr6 = @[stu4, stu5];
    NSArray *arr7 = @[arr5, arr6];
    NSLog(@"*********");
    // 遍历数组里每一个学生的姓名
    for (Student *str1 in arr5) {
        NSLog(@"%@", str1.name);
    }
    NSLog(@"*********");
    for (NSArray *str2 in arr7) {
        for (Student *str3 in str2) {
            NSLog(@"%@", str3.name);
        }
    }
        NSArray *testArr = @[@"1", @"2", @"3", @"4", @"5"];
    // 区别:
    // 上面是把数组每个元素的值直接赋给新的数组,即两者相同,数组元素个数为原数组元素个数(整形为元素)
    // 下面的是将原数组作为一个元素放入新数组中,新数组元素个数+1(数组为元素)
    NSArray *array = [[NSArray alloc] initWithArray:testArr];
    NSArray *array1 = [[NSArray alloc] initWithObjects:testArr, nil];
    NSLog(@"%@\n%ld", array, array.count);
    NSLog(@"%@\n%ld", array1, array1.count);
    NSArray *array2 = [NSArray arrayWithArray:testArr];
    NSArray *array3 = [NSArray arrayWithObjects:testArr, nil];
    // 可变数组
    // 定义一个空数组
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSMutableArray *arr1 = [NSMutableArray array];
    NSMutableArray *arr2 = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4", nil];
    // 添加一个字符串
    [arr2 addObject:@"ddada"];
    NSLog(@"%@", arr2);
    // 移除下标为2的字符串
    [arr2 removeObjectAtIndex:2];
    NSLog(@"%@", arr2);
    // 插入一个字符串
    [arr2 insertObject:@"adada" atIndex:2];
    NSLog(@"%@", arr2);
    // 替换一个字符串
    [arr2 replaceObjectAtIndex:2 withObject:@"3"];
    NSLog(@"%@", arr2);
    // 交换两个字符串
    [arr2 exchangeObjectAtIndex:0 withObjectAtIndex:4];
    NSLog(@"%@", arr2);
    // 清空数组
    [arr2 removeAllObjects];
    NSLog(@"%@", arr2);
    // 练习
    Book *book1 = [Book bookWithName:@"三国志" price:19.98];
    Book *book2 = [Book bookWithName:@"资治通鉴" price:76.50];
    Book *book3 = [Book bookWithName:@"洗冤录" price:38.80];
    NSMutableArray *bookArr = [NSMutableArray arrayWithObjects:book1, book2, book3, nil];
    Book *book4 = [Book bookWithName:@"飞鸟集" price:76];
    [bookArr addObject:book4];
    for (Book *temp in bookArr) {
        NSLog(@"书名:%@,价格:%g", temp.name, temp.price);
    }
    NSLog(@"************");
    [bookArr removeObject:book3];
    for (Book *temp in bookArr) {
        NSLog(@"书名:%@,价格:%g", temp.name, temp.price);
    }
    NSLog(@"************");
    for (Book *temp in bookArr) {
        if ([temp.name isEqualToString:@"飞鸟集"]) {
            temp.price = 100;
        }
        NSLog(@"书名:%@,价格:%g", temp.name, temp.price);
        // [temp.name stringByReplacingOccurrencesOfString:@"飞鸟集" withString:@"毛泽东语录"]
    }
    NSLog(@"************");
    // 找到价格在70到100间的书有多少本,然后把符合条件的书放在一个数组里
    NSMutableArray *bookArr0 = [NSMutableArray array];
    for (Book *temp in bookArr) {
        if (temp.price >= 70 && temp.price <= 100) {
            [bookArr0 addObject:temp];
        }
    }
    for (Book *temp in bookArr0) {
        NSLog(@"%@ %g", temp.name, temp.price);
    }
    // 找到价格是100的书,把书名改成论0语
    for (Book *temp in bookArr) {
        if (temp.price == 100) {
            temp.name = @"论语";
            NSLog(@"%@", temp.name);
        }
    }

    // 可变和不可变数组的切换
    NSArray *testArr = @[@"1", @"2", @"3", @"4", @"5"];
    NSMutableArray *muArr = [NSMutableArray arrayWithArray:testArr];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值