关闭

OC_学习_数组,字典,集合

666人阅读 评论(0) 收藏 举报
分类:
//
//  main.m
//  OC_学习_数组等等
//
//  Created by binMac on 13-12-6.
//  Copyright (c) 2013年 bin-mac. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    //初始化一个数组
    NSArray *array1 = [NSArray arrayWithObject:@"bin"];
    NSLog(@"array = %@",array1);//arrayWithObject只能添加一个元素
    
    NSArray *array2 = [NSArray arrayWithObjects:@"bin",@"ban", nil];//arrayWithObjects可以添加多个元素,且nil,不能删除
    NSLog(@"array = %@",array2);

    //用另一个数组初始化一个数组
    NSArray *array3 = array2;
    NSLog(@"array = %@,%p,%p",array3,array3,array2);
    NSArray *array3b = [NSArray arrayWithArray:array3];
    NSLog(@"array = %@,%p,%p",array3b,array3b,array2);
    
    //求数组的元素
    NSInteger arrayCount = [array3 count];
    NSLog(@"这个数组的元素有 %ld",arrayCount);
    
    //通过下标取数组的元素
    NSString *str = [array3 objectAtIndex:1];
    NSLog(@"str = %@",str);
    
    //追加数组元素
    NSArray *array4 = [array3 arrayByAddingObject:@"bgn"];//(一个元素)
    NSLog(@"array4 = %@",array4);
    NSArray *array5 = [array3 arrayByAddingObjectsFromArray:array1 ];//追加一个数组
    NSLog(@"array5 = %@",array5);
    
    
    //components  成分,
    //用指定的字符串将数组中的元素连接起来变为一个字符串
    NSString *str2 = [array5 componentsJoinedByString:@"*"];
    NSLog(@"str2 = %@",str2);
    
    //判断数组中是否含有一个对象
    BOOL isContain = [array5 containsObject:@"bin"];
    NSLog(@"isContain = %d",isContain);
    
    //判断一个是元素在数组中的下标
    NSInteger index = [array4 indexOfObject:@"bin" inRange:NSMakeRange(0, 1)];//可以不用inrange,在指定范围内存在的话就返回正确的值,
    NSLog(@"index = %ld",index);
    
    //可变数组--------------
    NSMutableArray *marray1 = [NSMutableArray array];//空数组
    NSLog(@"marray1 = %@",marray1);
    
    NSMutableArray *marray2 = [NSMutableArray arrayWithCapacity:6];//告诉编译器大概有6个元素,方便内存优化
    NSLog(@"marray2=%@",marray2);
    
    //添加元素
    NSMutableArray *marray3 = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",nil];
    NSLog(@"marray = %@",marray3);
    
    [marray3 addObject:@"d"];//在后面追加一个元素
    [marray3 insertObject:@"e" atIndex:3];//在指定位置添加一个元素
     NSLog(@"marray = %@",marray3);
    [marray3 removeObject:@"e"];//删除指定对象
    
    //数组的遍历
    for (int i = 0; i < [marray3 count]; i ++) {
        NSString *str = [marray3 objectAtIndex:i];
        NSLog(@"str = %@",str);
    }//上面的方法效率很低
    
    for (NSString *str3 in marray3) {
        NSLog(@"str3 = %@",str3);//在这里面不能修改数组的元素,无论增删改
    }
//    for in实际上是快速枚举,跟for循环意义上还是有区别的。
//    NSArray的枚举操作中有一条需要注意:对于可变数组进行枚举操作时,你不能通过添加或删除对象这类操作来改变数组容器。如果你这么做了,枚举器会很困惑,而你将得到未定义的结果。
    
    
    
    //字典
    //字典是以键值对的形式存在的,并且键值是成对出现的,不会单独只有键或只有值(字典的打印使用{},表示的)
    
    //初始化一个字典
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"a",@"k1",@"b",@"k2",@"c",@"k3", nil];
    NSLog(@"dic1 = %@",dic1);//必须成对初始化
    
    //输出字典的数量
    NSLog(@"这个字典的数量 = %lu",[dic1 count]);//%ld  也可以输出
    
    //获取全部的key和object
    NSArray *keyarray=[dic1 allKeys];
    NSArray *valuearray=[dic1 allValues];
    NSLog(@"keyarray=%@,valuearray=%@",keyarray,valuearray);
    
    
    //可变字典
    NSMutableDictionary *mdic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"iphone",@"k1",@"ipad",@"k2",@"pro",@"k3",@"ari",@"k4", nil];
    NSLog(@"mdic=%@",mdic);
    
    //把一个字典添加到另外一个字典后面
    NSDictionary *dic4=[NSDictionary dictionaryWithObject:@"mini" forKey:@"k5"];
    //可变字典的追加
    [mdic addEntriesFromDictionary:dic4];
    //直接添加键值对
    [mdic setValue:@"ipod" forKey:@"k6"];
    //通过key删除指定的value
    [mdic removeObjectForKey:@"k6"];
    //删除所有
//    [mdic removeAllObjects];
//     NSLog(@"mdic=%@",mdic);
    
    //批量删除一个字典内容
    NSArray *arrayk=[NSArray arrayWithObjects:@"k3",@"k4",@"k5", nil];
    [mdic removeObjectsForKeys:arrayk];
    NSLog(@"mdic=%@",mdic);

    //----字典的遍历-----//从后面开始遍历
    for (id key in mdic) {
        id object = [mdic objectForKey:key];
        NSLog(@"value = %@",object);
    }
    
    //NSSet集合
    //NSSet集合跟数组差不多,只不过集合中不能存放相同的对象.是一组单值对象的集合,并且被存入集 中的元素是无序的,同样也有NSMutableSet和NSSet;
    NSSet *set1 = [[NSSet alloc] initWithObjects:@"aaa",@"bbb",@"ccc", nil];
    NSSet *set2 = [[NSSet alloc] initWithObjects:@"aaa",@"bbb", nil];
    NSLog(@"set1 = %@",set1);//打印形式set = {()}
    
    //判断一个集合时候包含某个元素
    BOOL isContain1 = [set1 containsObject:@"aaa"];
    NSLog(@"isContain1 = %d",isContain1);
    
    //判断集合是否有交集
    BOOL isIntersert = [set1 intersectsSet:set2];
    NSLog(@"isIntersert = %d",isIntersert);
    
    //判断集合中得元素是否一样
    BOOL isEqual = [set2 isEqualToSet:set1];
    NSLog(@"isEqual = %d",isEqual);
    
    //判断set1是否为set2的子集
    BOOL isSubSet = [set2 isSubsetOfSet:set1];
    NSLog(@"isSubSet = %d",isSubSet);
    
    //向一个集合中追加一个元素
    NSSet *set3 = [set1 setByAddingObject:@"ddd"];
    NSLog(@"set3 = %@",set3);

    
    NSMutableSet *set4 = [NSMutableSet setWithObjects:@"1",@"2", nil];
    NSMutableSet *set5 = [NSMutableSet setWithObjects:@"1",@"3", nil];

    [set4 intersectSet:set5];//并集   set4 = {(1)}
    NSLog(@"set4 = %@",set4);
    [set4 unionSet:set5];
    NSLog(@"set4 = %@",set4);
    
    //[set4 minusSet:set5];//set4将去除set5中相同的元素
    //NSLog(@"set4 = %@",set4);
    
    return 0;
}



   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5207次
    • 积分:140
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:4篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档