taiyang2014

NSArray NSMutableArray

   //NSArray:不可变数组类
    
    //创建一个数组
//    NSString *string = @"iPhone";
//    NSArray *array = [NSArray arrayWithObject:string];
    
//    NSArray *array = [NSArray arrayWithObject:@"iPhone"];
    
    //数组内部存放的元素必须是对象类型
//    NSArray *array = [NSArray arrayWithObject:@"1"];
    
    //取数组中的元素,元素下标从0开始
//    NSLog(@"%@", array[0]);
    
    //取到数组的元素,并做转化
//    NSInteger a = [array[0] integerValue];
//    NSLog(@"%ld", a);
    
    //创建一个有多个元素的不可变数组
    //nil是数组结束的标识符
    //数组的元素不确定,必须要加nil
//    NSArray *array = [NSArray arrayWithObjects:@"aaa", @"bbb", @"ccc", nil];
    
    //数组越界,程序崩溃
    //array[3];

    
    //得到数组的元素个数(不计算最后的nil)
//    NSUInteger count = [array count];
//    NSLog(@"%lu", count);
    
    //打印数组的每个元素
//    for (int i = 0; i < [array count]; i++) {
//        NSLog(@"%@", array[i]);
//    }
    
    //快捷打印数组(查看每个元素)
//    NSLog(@"%@", array);
    
//    Person *person1 = [Person personWithName:@"zhangsan" age:12];
//    Person *person2 = [Person personWithName:@"lisi" age:14];
//    Person *person3 = [Person personWithName:@"wangwu" age:18];
//    
//    NSArray *array = [[NSArray alloc] initWithObjects:person1, person2, person3, nil];
//    
//    NSLog(@"%@", array);
//    NSLog(@"%@", [array[1] name]);
    
    //数组中存放的元素必须是对象,但是对象的类型可以不同
    
    //NSArray是一个不可变的数组,一旦被创建,就不能改变(元素个数不能增减,元素内容不能变化)
//    array[0] = @"123123";
    //不可变数组,实质是一个指针数组,存放的元素都是指针.不可变指的是,每个元素(指针)不能重定向,指针指向的内容可以更改
//    [person2 setName:@"123"];
//    NSLog(@"%@", array);
    
    //NSMutableArray:可变数组,NSArray的子类,可以增删改
    
    //创建一个可变数组
//    NSMutableArray * mArray = [[NSMutableArray alloc] initWithCapacity:0];
    
    //向数组中添加元素
    //@"aaa",person
    //一个一个加(加到数组最后一个元素后面)
//    [mArray addObject:@"aaa"];
    
//    //1
//    Person *person = [Person personWithName:@"xiaoming" age:18];
//    [mArray addObject:person];
//    //2
//    [mArray addObject:[Person personWithName:@"xiaoming" age:18]];
    
    //一起加(按照你给的数组里元素的顺序添加)
//    NSArray *array = [NSArray arrayWithObjects:@"aaa", person, nil];
    //array中的每一个元素取出来,添加到mArray
//    [mArray addObjectsFromArray:array];
    //把array(当成一个对象)添加到mArray中 //    [mArray addObject:array]; //    NSLog(@"%@", mArray);      //    //1 //    NSArray *array = [NSArray arrayWithObjects:@"aaa", person, nil]; //    [mArray addObjectsFromArray:array]; //    //2 //    [mArray addObjectsFromArray:[NSArray arrayWithObjects:@"aaa", [Person personWithName:@"xiaoming" age:18], nil]];           //    NSMutableArray * mArray = [[NSMutableArray alloc] initWithCapacity:0]; // //    //增 //    [mArray addObject:@"123"]; //    [mArray addObject:@"789"]; //    [mArray addObject:@"321"];//456      //    //删除数组中的一个元素 //    [mArray removeObjectAtIndex:1]; //    NSLog(@"%@", mArray); //     //    //删除数组中的所有元素 //    [mArray removeAllObjects]; //    NSLog(@"%@", mArray);      //    //修改某个元素的值 //    [mArray replaceObjectAtIndex:2 withObject:@"456"]; //    NSLog(@"%@", mArray); //     //    //交换两个元素 //    [mArray exchangeObjectAtIndex:1 withObjectAtIndex:2]; //    NSLog(@"%@", mArray);      //    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0]; //     //    for (int i = 0; i < 10; i++) { //        int a = arc4random() % 101 + 50; //        NSString *string = [NSString stringWithFormat:@"%d", a]; //        array[i] = string; //    } //     //    NSLog(@"%@", array); //     //    for (int i = 0; i < [array count] - 1; i++) { //        for (int j = 0; j < [array count] - 1 - i; j++) { //            int a1 = [array[j] intValue]; //            int a2 = [array[j + 1] intValue]; //            if (a1 > a2) { //                [array exchangeObjectAtIndex:j withObjectAtIndex:j + 1]; //            } //        } //    } //     //    NSLog(@"%@", array); //     //    //姓名的排序 //    NSArray *array1 = [array sortedArrayUsingSelector:@selector(compare:)]; //    NSLog(@"%@", array1);      //    Library *library = [[Library alloc] init]; //     //    Book *book1 = [Book bookWithName:@"西游记" price:100]; //    Book *book2 = [Book bookWithName:@"三国演义" price:200]; //    Book *book3 = [Book bookWithName:@"水浒传" price:300]; //    Book *book4 = [Book bookWithName:@"红楼梦" price:400]; //     //    [library addBook:book1]; //    [library addBook:book2]; //    [library addBook:book3]; //    [library addBook:book4]; //     //    [library removeBook:book3]; //     //    [library showBookList]; // //    [library modifyPriceOfBookWithName:@"红楼梦" price:401];               //NSNumber,把C语言中的基本数据类型,转化成OC中的对象类型     int i = 5;     NSNumber *num1 = [NSNumber numberWithInt:i];     NSLog(@"%@", num1);          int j = [num1 intValue];     NSLog(@"%d", j);           //    int a = 1; //    char b = 'A'; //    short c = 8; //    float d = 8.99; //    BOOL e = YES; //     //    NSArray *array = [NSArray arrayWithObjects: //                      [NSNumber numberWithInt:a], //                      [NSNumber numberWithChar:b], //                      [NSNumber numberWithShort:c], //                      [NSNumber numberWithFloat:d], //                      [NSNumber numberWithBool:e], //                      nil]; //     //    NSLog(@"%@", array);                    //快捷的方式转NSNumber //    NSArray *array1 = [NSArray arrayWithObjects:@1, @'A', @8, @8.99, @(YES), nil]; //    NSLog(@"%@", array1);          //NSArray *array3 = [NSArray arrayWithObjects:@"aaa", @"bbb", @"ccc", nil];     //快捷的方法,创建一个数组 //    NSArray *arra3 = @[@"aaa", @"bbb", @"ccc"]; //    NSLog(@"%@", arra3);          //语法糖(syntactic sugar)     //@ + 数字 : 转成NSNumber     //@[元素1,元素2,元素3,...] : 创建一个数组      //    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0]; //    这个是可以比较出来的因为NSNumber中compare返回的是较大的那个值 //    for (int i = 0; i < 10; i++) { //        int temp = arc4random() % 100; //        array[i] = [NSNumber numberWithInt:temp]; //    } //     //    NSLog(@"%@", array); //     //    for (int i = 0; i < [array count] - 1; i++) { //        for (int j = 0; j < [array count] - 1 - i; j++) { //            if ([array[j] compare:array[j + 1]] == NSOrderedDescending) { //                [array exchangeObjectAtIndex:j withObjectAtIndex:j + 1]; //            } //        } //    } //     //    NSLog(@"%@", array);          int array[5] = {9, 1, 2, 3, 4};          BOOL flag = YES;//flag为YES,表示上次排序进行了交换, NO表示上次排序没有做交换     for (int i = 0; i < 5 - 1 && flag; i++) {         flag = NO;//假设本趟循环没有作交换         for (int j = 0; j < 5 - 1 - i; j++) {             if (array[j] > array[j + 1]) {                 int temp = array[j];                 array[j] = array[j + 1];                 array[j + 1] = temp;                 //如果交换,就把flag置为YES                 flag = YES;             }         }     }

 

posted on 2015-03-02 19:26  taiyang2014  阅读(535)  评论(0编辑  收藏  举报

导航