个人整理--OC中的数组

//OC中的数组存放的一定是对象
NSArray *arr = [[NSArray alloc]init];
//用便利构造器的方式创建一个空数组
NSArray *arr = [NSArray array];
//字面量的方式
NSArray *arr = @[@"1",@"2",@"3",@"4",@"5"];
//.count:能够读出数组中元素个数.
NSLog(@"%ld",arr.count);
//取值也是通过下标进行取值,返回的是一个对象;
NSLog(@"%@",[arr objectAtIndex:1]);
NSLog(@"%@",arr[1]);
//for 对数组进行遍历
for (NSInteger i = 0 ; i < arr.count ; i ++){
NSLog(@"%@",arr[i]);
}
NSLog(@"%@",[arr containsObject:@"6"]);
 Student *stu1 = [[Student alloc]initWithName:@"刘鑫奇"];
 Student *stu2 = [[Student alloc]initWithName:@"高薪茹"];
 Student *stu3 = [[Student alloc]initWithName:@"高小妖精"];
 Student *stu4 = [[Student alloc]initWithName:@"高大贱人"];
 NSArray *arr = [[NSArray alloc]initWithObjects:stu1,stu2,stu3,stu4, nil];
    //用便利构造器的方式创建
NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];
    //快速枚举:能快速的遍历数组等容器对象
    //都是对容器中的每一个元素的遍历
    //为了增加代码的阅读性,避免不必要的错误,尽量让 forin的前部分的类型和数组里元素类型相同
  for (NSString *str in arr) {
        NSLog(@"%@",str);
   }
   NSArray *arr1 = @[@"刘鑫奇",@"刘山山",@"刘星宇",@"刘能"];
   NSArray *arr2 = @[@"腾飞",@"周胜民",@"商帅",@"郭洪瑞"];
   NSArray *arr = @[arr1 , arr2];
//对arr进行forin遍历
    for (NSArray *temp in arr){
        for (NSString *str in temp){
            NSLog( @"%@",str);
        }
    }
    Student *stu1 = [[Student alloc]initWithName:@"刘鑫奇"];
    Student *stu2 = [[Student alloc]initWithName:@"高薪茹"];
    Student *stu3 = [[Student alloc]initWithName:@"高小妖精"];
    Student *stu4 = [[Student alloc]initWithName:@"高大贱人"];
    NSArray *arr1 = @[stu1,stu2,stu3];
    NSArray *arr2 = @[stu4];
    NSArray *arr = @[arr1 , arr2];
    //遍历数组里每一个学生的姓名
    for (Student *temp in arr) {
        for (Student *stu in temp) {
            NSLog(@"%@",stu.name);
            }
    }
 //可变数组
     NSMutableArray *arr = [[NSMutableArray alloc]init];
     NSMutableArray *arr1 = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil];
//添加一个字符串
//添加到数组的最后一位
    [arr addObject:@"8"];
    NSLog(@"%@",arr);
//移除下标2的字符串
    [arr removeObjectAtIndex:2];
    NSLog(@"%@",arr);
//替换一个字符串
    [arr replaceObjectsAtIndexes:1 withObject:@"7"];
    NSLog(@"%@",arr);
//交换两个字符串
    [arr exchangeObjectAtIndex:1 withObjectAtIndex:4];
    NSLog(@"替换%@",arr);
//清空数组
    [arr removeAllObjects];
    NSLog(@"清空%@",arr);
    Book *book1 = [Book bookWithbookName:@"西游记" bookPrice:30];
    Book *book2 = [Book bookWithbookName:@"水浒传" bookPrice:25];
    Book *book3 = [Book bookWithbookName:@"三国演义" bookPrice:35];
    Book *book4 = [Book bookWithbookName:@"金瓶梅(插图版)" bookPrice:40];
    //四本书放在一个可变数组中
    NSMutableArray *bookArr = [NSMutableArray arrayWithObjects:book1,book2,book3,book4, nil];
    //    2、数组可以添加、删除书籍。
    Book *book5 = [Book bookWithbookName:@"红楼梦" bookPrice:38];
    [bookArr addObject:book5];
    for (Book *book in bookArr) {
        NSLog(@"%@ , %ld",book.bookName,book.bookPrice);
    }
    [bookArr removeObject:book5];
    for (Book *book0 in bookArr) {
        NSLog(@"%@ ,%ld ",book0.bookName , book0.bookPrice);
    }
    for (Book *temp in bookArr) {
       if ([temp.bookName isEqualToString:@"红楼梦"]) {
            temp.bookPrice = 150;
        }
    }
    for (Book *temp in bookArr) {
       NSLog(@"%@,%ld",temp.bookName,temp.bookPrice);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值