您的位置:首页 > 其它

oc- 字典 集合

2015-08-20 17:00 197 查看
#import <Foundation/Foundation.h>

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

@autoreleasepool {

//by2的什么来着 《永远在身边》 <Daydream>-super junior

#pragma mark ----- 不可变字典 ------------

//1、字典元素是以 key : value 存储的。

//2、字典中的元素存放是无序的。

//3、字典中存取值都是通过 key 来搭桥。

//4、字典中的 key 值是不允许重复的,但是 value 值是可以重复的。

//5、字典也有可变和不可变之分。

//注意:如果,一旦 key 值有重复的,会自动放弃后面重复的,只保留第一次遇到的那个 key : value 对。

//{ }

//==》1、创建字典对象。

NSArray *arrayKey=@[@"one",@"two",@"three",@"four"];

NSArray *arrayValue=@[@"apple",@"banana",@"orange",@"pear"];

//==>通过数组给key以及value赋值

NSDictionary *dic1=[[NSDictionary alloc]initWithObjects:arrayValue forKeys:arrayKey];

NSLog(@"dic1字典:%@",dic1);

//==>通过字典给字典赋值

NSDictionary *dic2=[[NSDictionary alloc]initWithDictionary:dic1];

NSLog(@"dic2字典:%@",dic2);

//==>通过顺序给字典赋值-成对写:value,key

NSDictionary *dic3=[[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"one",@"2",@"two", nil];

NSLog(@"dic3字典:%@",dic3);

//==>通过 字面量(@{}形式叫字面量) 赋值

NSDictionary *dic4=@{@"1":@"one",@"2":@"two",@"3":@"three",@"4":@"three"};

NSLog(@"dic4字典:%@",dic4);

//===》2、获取key 对应的 value 值

NSLog(@"dic1字典中one对应的value值:%@",[dic1 objectForKey:@"one"]);

//===》2、两种根据key 获取的内容--之后查一查两者的区别 前者获取对象,后者获取值

NSLog(@"第一种:[dic4 objectForKey:key]=%@",[dic4 objectForKey:@"2"]);

NSLog(@"第二种:[dic4 valueForKey:key]=%@",[dic4 valueForKey:@"2"]);

//===》2、再一种根据key获取value值

NSLog(@"第三种:dic4[key]=%@",dic4[@"2"]);

//===》3、获取所有的key

NSLog(@"dic4字典中所有的key:%@",[dic4 allKeys]);

//===》4、获取所有的value

NSLog(@"dic4字典中所有的value:%@",[dic4 allValues]);

//===》5、运算方面

//==>计算元素个数 --key :value 有多少对

NSLog(@"[dic1 count]=%lu",[dic1 count]);

NSLog(@"[dic2 count]=%lu",[dic2 count]);

NSLog(@"[dic3 count]=%lu",[dic3 count]);

NSLog(@"[dic4 count]=%lu",[dic4 count]);

//===》6、获取相同value值对应的多个不同的key值

//因为字典允许一对一,以及一对多,所以在获取value值的时候有可能会对应好几个key值,key值是不重复的。

[dic4 allKeysForObject:@"2"];

NSLog(@"返回three对应的key值:%@",[dic4 allKeysForObject:@"three"]);

#pragma mark ----- 可变字典 ---------

//可变的 继承自 不变的。

//可变的所做的操作都是对自身的操作,而不变的是不能有所改变的,所以对不变的操作都是返回一个新的字典。

//===》1、创建可变字典对象

NSMutableDictionary *NSMdic1=[[NSMutableDictionary alloc]initWithObjects:arrayValue forKeys:arrayKey];

NSLog(@"NSMdic1字典:%@",NSMdic1);

//既然是继承的,当然可以使用不变字典中的方法

//===》1、根据便利构造器创建字典对象

NSMutableDictionary *NSMdic2=[NSMutableDictionary dictionaryWithObjects:arrayValue forKeys:arrayKey];

NSLog(@"NSMdic2字典:%@",NSMdic2);

//获取allkeys

NSLog(@"allkeys:%@",[NSMdic2 allKeys]);

//获取allvalue

NSLog(@"allvalues:%@",[NSMdic2 allValues]);

//===》2、count

NSLog(@"count:%lu",[NSMdic2 count]);

//===》3、增删查改

//===》3、添加 key:value 对

[NSMdic1 setObject:@"peach" forKey:@"five"];

NSLog(@"afterAdd:%@",NSMdic1);

//===》3、删除

[NSMdic1 removeObjectForKey:@"five"];

NSLog(@"afterRemove:%@",NSMdic1);

//====》3、查找--遍历

NSLog(@"showAllElements:");

NSArray *arrayKeys=[NSMdic1 allKeys];

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

NSLog(@"key[%d]=%@ value[%d]=%@",i,arrayKeys[i],i,NSMdic1[arrayKeys[i]]);

// [NSMdic1 objectForKey:arrayKeys[i]];也是一样可以得到key对应的value值

}

#pragma mark ----- 不可变集合 -----

//存储元素互不相同。

//存储的元素是无序的。

//存储的元素必须是对象。

// { ( )}

//====》1、创建

NSSet *set1=[[NSSet alloc]initWithObjects:@"q",@"w",@"e",@"r", nil];

NSLog(@"set1:%@",set1);

//====》1、

NSSet *set2=[[NSSet alloc]initWithArray:arrayValue];

NSLog(@"set2:%@",set2);

//====》1、

NSSet *set3=[[NSSet alloc]initWithSet:set2];

NSLog(@"set3:%@",set3);

//====》2、count

NSLog(@"[set1 count]:%lu",[set1 count]);

//====》3、随机获取一个元素

NSLog(@"[set1 anyObject]=%@",[set1 anyObject]);

//判断集合中是否包含某个元素 --返回BOOL型结果

NSLog(@"是否包含:%d",[set1 containsObject:@"q"]);

//====》4、所有的元素allObjects--返回类型是一个数组,数组用()包住

NSLog(@"allObjects:%@",[set1 allObjects]);

//====》5、通过allObjects获取到的数组来得到单个元素

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

NSLog(@"第%d个元素:%@",i,[set1 allObjects][i]);

}

#pragma mark ---- 可变集合 -----------

//可变集合 继承自 不可变集合

//1、创建

NSMutableSet *NSMset1=[[NSMutableSet alloc]initWithArray:arrayKey];

NSLog(@"NSMset1:%@",NSMset1);

//1、

NSMutableSet *NSMset2=[[NSMutableSet alloc]initWithObjects:@"z",@"x",@"c",@"v", nil];

NSLog(@"NSMset2:%@",NSMset2);

//1、

NSMutableSet *NSMset3=[[NSMutableSet alloc]initWithSet:NSMset2];

NSLog(@"NSMset3:%@",NSMset3);

//1、便利构造器

NSMutableSet *NSMset4=[NSMutableSet setWithArray:arrayKeys];

NSLog(@"NSMset4:%@",NSMset4);

//setSet===》Empties the receiving set,then adds each object contained in another given set.

NSLog(@"没有setSet之前的NSMset4:%@",NSMset4);

[NSMset4 setSet:NSMset2];

NSLog(@"setSet后的NSMset4:%@",NSMset4);

#pragma mark ------- NSCountedSet -----------------

//NSCountedSet 继承自 NSMutableSet.

//计数用的。

//===》1、创建

NSCountedSet *cSet=[NSCountedSet setWithArray:arrayValue];

NSLog(@"cSet:%@",cSet);

//====》2、添加

[cSet addObject:@"hey"];

NSLog(@"afterAdd:%@",cSet);

//====》3、得到集合中某个元素的个数

NSUInteger count=[cSet countForObject:@"hey"];

NSLog(@"count:%lu",count);

//练习:给一个随机数组,除去数组中重复出现的数字。

NSMutableArray *nsmArray=[NSMutableArray array];

for (int i=0; i<5; i++) {

//接收随机数 转换成数值对象。

NSNumber *number=[[NSNumber alloc]initWithInt:(arc4random()%(9-2+1)+2)];

//把数值对象 添加到 可变数组中。

[nsmArray addObject:number];

}

NSLog(@"源数组:%@",nsmArray);

//定义一个可变集合,用来装可变数组中的对象

//现在有计数了,只需要判断个数>1?YES:NO;

NSCountedSet *cSet1=[[NSCountedSet alloc]initWithArray:nsmArray];

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

//每次遍历过后nsmArray都在改变

NSCountedSet *cSet1=[[NSCountedSet alloc]initWithArray:nsmArray];

//得到每个元素对象的个数

NSUInteger count=[cSet1 countForObject:nsmArray[i]];

if (count>1) {

//定义一个数值对象来记录重复数这个对象

NSNumber *temp=nsmArray[i];

//删除这个重复数

[nsmArray removeObject:nsmArray[i]];

//添加这个重复数到可变集合的末尾

[nsmArray addObject:temp];

//此时要回到起始位置开始判断

i--;

}

}

NSLog(@"删除重复数后;%@",nsmArray);

#pragma mark ------- 快速遍历 ----------------

//遍历数组

NSLog(@"遍历数组");

NSArray *arr1=[[NSArray alloc]initWithArray:arrayValue];

for (id obj in arr1) {

NSLog(@"%@",obj);

}

//遍历字典 --每次返回的都是key值

NSLog(@"遍历字典");

NSDictionary *dict1=[[NSDictionary alloc]initWithObjects:arrayValue forKeys:arrayKey];

for (id obj in dict1) {

NSLog(@"%@,%@",obj,dict1[obj]);

}

#pragma mark ------- 枚举器 -----------------

//遍历数组

NSEnumerator *enum1=[arrayValue objectEnumerator];

id obj;

while (obj = [enum1 nextObject]) {

NSLog(@"正在遍历%@",obj);

}

NSLog(@"遍历结束");

//遍历字典

NSEnumerator *enum2=[dic4 objectEnumerator];

id objDict;

while (objDict = [enum2 nextObject]) {

NSLog(@"正在遍历%@",objDict);

}

NSLog(@"遍历结束");

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: