oc学习之NSArray与NSMutableArray数组

//
//  main.m
//  6-10复习
//
//  Created by kj on 14-6-10.
//  Copyright (c) 2014年 kj. All rights reserved.
//

#import 

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

    @autoreleasepool {
        //创建数组对象,NSArray不可变数组
        //c语言的数组可以存放任何类型的数据,同一个数组只能有同一种数据类型
        //oc语言的数组只能存放对象,可以存放任意对象
        // [NSNull null] == 空对象
        //NSArray *arr = [[NSArray alloc] initWithObjects:@"123",@"345",@"abc", nil];
        NSArray *arr1 = @[@"1 2 3", @"345", [NSNull null], @"789"];
        NSArray *arr3 = [[NSArray alloc] initWithArray:arr1];
        NSLog(@"arr = %@",arr3);
        
        //将c数组转换为oc数组对象
        //oc不允许将nil插入到数组中,因为nil作用是数组的结束标记
        NSString *carr[10] = {@"123", @"456", @"789"};
        NSArray *arr4 = [[NSArray alloc] initWithObjects:carr count:2];
        NSLog(@"arr4 = %@",arr4);
        //获取元素个数
        NSUInteger count = [arr4 count];
        NSLog(@"count = %ld",count);
        //获取指定下标元素
        NSLog(@"%@",[arr3 objectAtIndex:1]);
        //获取第一个元素或者最后一个元素
        id arrf = [arr3 firstObject];
        id arrl = [arr3 lastObject];
        NSLog(@"%@ %@",arrf,arrl);
        //获取子集
        NSArray *arr2 = @[@"111", @"222", @"333", @"444", @"555", @"666"];
        
        //取出连续范围的元素作为子集内容
        NSArray *sub1 = [arr2 subarrayWithRange:NSMakeRange(1, 3)];
        
        NSLog(@"sub1 = %@", sub1);
        //获取任意元素作为子集
        //1.列出所需要元素的下标清单
        NSMutableIndexSet *indexs = [NSMutableIndexSet indexSet];
        //1-1.将要获取元素的下标写入清单
        for (NSInteger i = 0; i < [arr2 count]; i++)
        {
            if (i % 2 != 0)
            {
                [indexs addIndex:i];
            }
        }
        
        //2.将清单交给数组
        //id obj =[arr2 objectAtIndex:0]; //获取单个元素,返回单个元素
        NSArray *sub2 = [arr2 objectsAtIndexes:indexs];//获取下标清单里的所有元素,返回一个元素集合(数组)
        
        NSLog(@"sub2 = %@", sub2);
        
        //获取最长字符串和最小字符串
        //快速枚举
        NSArray *arr5 = @[@"123456789", @"222", @"333", @"errr", @"444", @"5", @"666"];
        NSInteger maxLength = [arr5[0] length];
        NSInteger minLength = [arr5[0] length];
        NSString *maxString = nil;
        NSString *minString = nil;
        
        for (id obj in arr5) {
            
            if (maxLength <= [obj length]) {
                
                maxLength = [obj length];
                maxString = obj;
            }
            
            if (minLength >= [obj length]) {
                
                minLength = [obj length];
                minString = obj;
            }
        }
        NSLog(@"maxString = %@, minString = %@",maxString,minString);
#if 0
        //创建可变数组对象
        NSMutableArray *arr1 = [[NSMutableArray alloc] initWithCapacity:30];
        NSMutableArray *arr2 = [NSMutableArray arrayWithCapacity:30];
        
        //添加元素
        [arr1 addObject:@"123"];
        [arr1 addObject:@"456"];
        
        [arr2 addObjectsFromArray:arr1];
        [arr2 addObject:@"789"];
        [arr2 addObject:@"789"];
        [arr2 addObject:@"789"];
        
        NSLog(@"arr1 = %@",arr1);
        NSLog(@"arr2 = %@",arr2);
        
        //插入元素
        //插入下标的范围: 0<=index<=count
        [arr1 insertObject:@"aaa" atIndex:1];
        [arr1 insertObject:@"bbb" atIndex:[arr1 count]];
        NSLog(@"arr1 = %@",arr1);
        
        //删除
        [arr2 removeAllObjects];//删除所有元素
        [arr2 removeLastObject];//删除最后一个元素
        [arr2 removeObject:@"789"];//删除指定对象,会将所有对象删除
        [arr2 removeObject:@"789" inRange:NSMakeRange(1, 2)];//删除指定范围中的指定对象
        [arr2 removeObjectAtIndex:0];//删除指定下标的元素
        [arr2 removeObjectsInRange:NSMakeRange(2, 2)];//删除指定范围中得元素
        [arr2 removeObjectIdenticalTo:@"789"];
        [arr2 removeObjectIdenticalTo:@"789" inRange:NSMakeRange(2, 1)];
        //如果要删除不连续的元素,则先把要删除的元素放在一个清单中,然后再删除该清单
        //比如要删下标为偶数的
        
        NSMutableIndexSet *indexs = [NSMutableIndexSet indexSet];
        
        for (int i = 0; i < [arr2 count]; i++) {
            
            if (i % 2 == 0) {
                
                [indexs addIndex:i];
            }
            
        }
        //删除清单中得元素
        [arr2 removeObjectsAtIndexes:indexs];
        
        
        //修改数组中得内容
        [arr2 replaceObjectAtIndex:2 withObject:@"aaa"];
        NSArray *arr = @[@"qqqq",@"www",@"ee"];
        [arr2 replaceObjectsAtIndexes:indexs withObjects:arr];
        // qq, 789, ww, 456, ee
         [arr2 replaceObjectsInRange:NSMakeRange(1, 3) withObjectsFromArray:arr];// qq, qq, ww, ee, ee,在指定范围内,修改数组中得内容。
        
        //交换数组中得内容
        [arr2 exchangeObjectAtIndex:1 withObjectAtIndex:3];
        //用oc对c语言中得数组进行排序
        for (int i = 0; i < [arr2 count]; ++i) {
            
            for (int j = i + 1; j < [arr2 count]; ++j) {
                
                if ([arr2[i] length] > [arr2[j] length]) {
                    
                    [arr2 exchangeObjectAtIndex:i withObjectAtIndex:j];
                }
            }
        }
        NSLog(@"arr2 = %@",arr2);
#endif
    }
    return 0;
}


你可能感兴趣的:(OC学习笔记)