• 题 数组 添加 删除 排序 按要求输出


    #define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

    #import <Foundation/Foundation.h>

    int main(int argc, const char * argv[]) {

        @autoreleasepool {

            NSArray *arr=@[@{@"name":@"Tim Cook",@"age":@"24",@"sex":@"female",@"score":@"89"},@{@"name":@"Jony Ive",@"age":@"26",@"sex":@"female",@"score":@"76"},@{@"name":@"Steve Jobs",@"age":@"24",@"sex":@"male",@"score":@"67"},@{@"name":@"Robert Brunne",@"age":@"28",@"sex":@"male",@"score":@"88"}];

            

            /*

             1.添加数据姓名:Philip Schiller年龄:29性别:female分数:70到arr数组内。

             2.查找数组内"Steve Jobs"的数据并删除。

    //法2

     for (int i=0;i<[mutarray count];i++) {

                NSDictionary *dic=mutarray[i];

                if([ [dic objectForKey:@"name"] isEqual:@"Steve Jobs"])

                {

                    [mutarray removeObject:dic];

                }

            }

            for (NSDictionary *dic1 in mutarray) {

                NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dic1[@"name"],dic1[@"age"],dic1[@"sex"],dic1[@"score"]);

            }

             3.按姓名首字母进行排序。

             4.按年龄进行升序排序,如果年龄相同则按性别进行排序。

             5.输出成绩大于或等于80分的学员信息。*/

            

        //1.添加数据姓名:Philip Schiller年龄:29性别:female分数:70到arr数组内。

            

            NSMutableArray *mutarray=[[NSMutableArray alloc]initWithArray:arr];

            

            [mutarray addObject:@{@"name":@"Philip Schiller",@"age":@"29",@"sex":@"female",@"score":@"70"}];

            

            for(id str in mutarray){

                

                NSLog(@"%@",str);

                

            }

            //2.查找数组内"Steve Jobs"的数据并删除。

            NSLog(@"查找数组Steve Job的数据并删除");

            NSArray *arr0=[[NSArray alloc]initWithArray:mutarray];

            for (id dic in arr0) {

                

                if([ [dic objectForKey:@"name"] isEqual:@"Steve Jobs"])

                    

                {

                    [mutarray removeObject:dic];

                }

            }

            for (NSDictionary *dic1 in mutarray) {

                

                NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dic1[@"name"],dic1[@"age"],dic1[@"sex"],dic1[@"score"]);

            }

            

            NSLog(@" ");

          //3.按姓名首字母进行排序。

            NSLog(@"按姓名首字母进行排序");

               NSSortDescriptor *p1=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:1];

               NSArray *p=[mutarray sortedArrayUsingDescriptors:[NSArray arrayWithObject:p1]];

            for (NSDictionary *dict in p) {

                

                    NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);

            }

            NSLog(@" ");

            //4.按年龄进行升序排序,如果年龄相同则按性别进行排序。

            NSLog(@"按年龄进行升序排序,如果年龄相同则按性别进行排序");

            NSSortDescriptor *p3=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:1];

            

            NSSortDescriptor *p4=[NSSortDescriptor sortDescriptorWithKey:@"sex" ascending:1];

            

            NSArray *p5=[mutarray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:p3,p4, nil]];

            for (NSDictionary *dict in p5) {

                

                NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);

            }

            

            NSLog(@" ");

            

            //5.输出成绩大于或等于80分的学员信息。

            

            

            for (NSDictionary *dict in mutarray) {

                

                int b=[dict[@"score"] intValue];

                if(b>80){

                    

                    NSLog(@"姓名:%@,年龄:%@,性别%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);

                }

                

            }

         

        }

        return 0;

    }

  • 相关阅读:
    Bootstrap表单验证插件bootstrapValidator使用方法整理
    去掉表格前符号
    消除float浮动的影响
    html 让一行文字居中
    java通过各种类型驱动连接数据库
    命令行查看端口
    You can't specify target table 'table' for update in FROM clause
    mysql 添加字段 修改字段为not null
    Js、Jquery定时执行(一次或者重复多次,取消重复)
    c# datetime与 timeStamp(unix时间戳) 互相转换
  • 原文地址:https://www.cnblogs.com/bobohahaha/p/5121709.html
Copyright © 2020-2023  润新知