字典练习

1.创建一个字典,里面包含班级的各个小组,然后每个小组中又包含这组所有成员的名字,
        
        NSArray *arr1 = [[NSArray alloc] initWithObjects:@"小敏",@"小飞飞",@"小俊珍", nil];
        NSArray *arr2 = [[NSArray alloc] initWithObjects:@"小黑",@"大白",@"胖墩", nil];
        NSArray *arr3 = [[NSArray alloc] initWithObjects:@"啦啦",@"哈哈",@"嘎嘎",@"嘿嘿", nil];
        NSArray *arr4 = [[NSArray alloc] initWithObjects:@"大家",@"你呢",@"但是",@"反而", @"fennea",nil];

        NSMutableDictionary *team = [[NSMutableDictionary alloc] initWithObjectsAndKeys:arr1, @"第一组",arr2,@"第二组",arr3,@"第三组",arr4,@"第四组",nil];
        NSArray *class = team.allKeys;
        for (int i = 0; i < class.count; i++) {
         NSArray *class1 = [team valueForKey:class[i]];
         for (int j = 0; j < class1.count; j++) {
                 NSLog(@"mdicKey1-Value1= %@ -->%@",class[i],class1[j]);
           }
           
        }


2.定义一个字典,字典是一个学生的信息:姓名、年龄、学号、地址.通过遍历显示学生的所有信息
        NSArray *stu11 = [[NSArray alloc] initWithObjects:@"小飞飞",@"女",@"11",@"1",@"洛阳", nil];
        NSArray *stu22 = [[NSArray alloc] initWithObjects:@"妖怪",@"男",@"10" @"6",@"迷失之城",nil];
        NSArray *stu33 = [[NSArray alloc] initWithObjects:@"神仙",@"男",@"13",@"3",@"昆仑山", nil];
        NSArray *stu44 = [[NSArray alloc] initWithObjects:@"小敏",@"女" ,@"18",@"4",@"周口", nil];
        
        NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:stu11,@"1",stu22,@"2",stu33,@"3",stu44,@"4", nil];
        
        NSArray *key1 = message.allKeys;
        for (int i = 0; i < key1.count; i++) {
           NSArray *value1 = [message valueForKey:key1[i]];
           for (int j = 0; j < value1.count; j++) {
            NSLog(@"%@ ->%@",key1[i],value1[j]);
            }
        }

3.定义一个数组,数组中是student对象,按学生的 学号/年龄/姓名 排序

 Student *stu1 = [Student studentWithName:@"xiao小飞飞" withSex:@"女" age:12 number:1 adress:@"洛阳"];
        Student *stu2 = [Student studentWithName:@"Yao妖怪" withSex:@"男" age:19 number:3 adress:@"迷失之城"];
        Student *stu3 = [Student studentWithName:@"Shen神仙" withSex:@"男" age:16 number:2 adress:@"昆仑山"];
        Student *stu4 = [Student studentWithName:@"Min小敏" withSex:@"女" age:13 number:7 adress:@"周口"];
        NSMutableArray *Array = [NSMutableArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];        

 NSLog(@"----------------------------------按年龄进行排序");
        for (int i = 0; i < Array.count - 1; i++) {
        for (int j = 0; j < Array.count - 1 - i; j++) {
            if ([Array[j] age] > [Array[j + 1] age]) {
                [Array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
        }
       }
      }
        for (int i = 0; i < Array.count; i++) {
            NSLog(@"姓名 = %@ 学号 = %d 性别 = %@ 年龄= %d 地址 = %@",[Array[i] name],[Array[i] number],[Array[i] sex],[Array[i] age],[Array[i] adress]);
        }
        NSLog(@"----------------------------------按学号进行排序");
        for (int i = 0; i < Array.count - 1; i++) {
        for (int j  = 0; j < Array.count - 1 - i; j++) {
         if ([Array[j] number] > [Array[j + 1] number]) {
         [Array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
                }
            }
        }
        for (int i = 0; i < Array.count; i++) {
            NSLog(@"姓名 = %@ 学号 = %d 性别 = %@ 年龄= %d 地址 = %@",[Array[i] name],[Array[i] number],[Array[i] sex],[Array[i] age],[Array[i] adress]);
        }
         NSLog(@"----------------------------------按姓名进行排序");
        for (int i = 0; i < Array.count - 1; i++) {
        for (int j = 0; j < Array.count - 1 - i; j++) {
        if ([[Array[j] name] compare:[Array[j + 1] name]] == NSOrderedDescending) {
         [Array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
                }
            }
        }
        for (int i = 0; i < Array.count; i++) {
            NSLog(@"姓名 = %@ 学号 = %d 性别 = %@ 年龄= %d 地址 = %@",[Array[i] name],[Array[i] number],[Array[i] sex],[Array[i] age],[Array[i] adress]);
        }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值