数组的方式(顺序存储)
NSArray类
数组里面只能存放对象.
放基本数据类型需要转换.
(int value)
1.可以存放多个对象
2.每个对象使用下标来表示
3.最后以nil结束
Student*zhao=[Student studentWithName:@"赵云" andAge:22 and Hobby:@"起码" andAddress:@"guangzhou"];
然后下面重复 学生ma.学生cui.学生熊
然后用数组组合起来
Student*array[
遍历NSArray
也分为可变数组 不可变数组
NSMutabelArray
是NSArray的子类 继承了NSArray的方法.
扩展了多个方法,实现了添加删除修改的功能
+arrayWithCapacity
-initWithCapacity
-removeAllobjects
-remobeObjectAtIndex: (移除..下标不要越界 会立刻崩溃))
-replaceObjectAtIndex:withObject;
用一个数组初始化另外一个数组
NSArray*array4=[[NSArray arrayWithArray :array3]]; NSLog(@"array4=%@",array4);
字典
字典和数组一样,都是储存数据的容器,能够储存多个数据,只是和数组的储存方式不同,数组是按下标来储存数据的,字典是按键值对储存的
Key-Value
键
值
键值对!
字典中没有存储数据,使用Key来表示每一个对象
cocoa框架中的字典:NSDictionary和NSMutableDictionary!
用Key来取Value
一个key只能对于value来说只能是一个
值却可有拥有很多把key
key是唯一的
value可以重复
(一个钥匙只能开一把锁 一把锁可以有很多把钥匙)
打印是以大括号的形式打印
字典里面包括数组 可嵌套.
使用NSDictionary的方法
创建Dictionary对象
+dictionarywithobjectsandkey;//创建键值对象
获取对象
-objectForKey //id
获取所有的key
-allKeys
获取所有对象(值)
-allValue
/=========初始化一个数组
//创建几个字符串对象
NSArray*array1=[[NSArrayalloc]initWithObjects:@"iPhone",@"iPad",@"iOS",@"Mac. Pro",@"Mac. Air",nil];
NSLog(@"array1=%@",array1);//在控制台上数组的打印形式为()小括号,每个元素之间用逗号隔开
[array1 release];
//试试一个字符串行否 以objects(s)结尾的要加nil
NSArray*array2=[[NSArrayalloc]initWithObjects:@"iPhone",nil];
NSLog(@"array1=%@",array2);
//内部自动封装不用手动管理内存 (便利构造器)
//iOS6.0之后才出的新方法属于便利构造器的方法
NSArray*array3=@[@"iPhone",@"iPad",@"iPod",@"Mac. Pro"];
NSLog(@"array3=%@",array3);
NSArray*array4=[NSArrayarrayWithArray:array3];
NSLog(@"array4=%@",array4);
//用.count也可以 .length.用点来调.
NSLog(@"array4的元素个数为%d",[array4 count]);
//根据索引取数组元素方法索引从0开始
NSArray*str=[array4 objectAtIndex:1];
NSLog(@"str=%@",str);
//追加多个元素,在后面. (arrayByAddingObject是追加一个元素
NSArray*array5=[array4arrayByAddingObjectsFromArray:array3];
NSLog(@"array5=%@",array5);
//追加元素在后面添加
NSArray*array6=[array3 arrayByAddingObject:array2];
NSLog(@"array6=%@",array6);
//找到对象的索引(根据对象查找索引的方法)
NSInteger index= [array5 indexOfObject:@"iPad"];
NSLog(@"index=%d",index);
// (NSString *)componentsJoinedByString:(NSString *)separator;
//没有小括号就不是数组的形式了分隔符也可以不写
NSString *str2=[array4componentsJoinedByString:@","];
NSLog(@"str2=%@",str2);
//数组的遍历.正常的循环
for (int i=0; i<[array4count]; i++) {
NSString*str3=[array4 objectAtIndex:i];
NSLog(@"str3=%@",str3);
}
//for (指针对象 in 数组);
//不用管创建了多少次.快速枚举!
//效率高.
for (NSString*str3in array4)
{
NSLog(@"str4=%@",str3);
}
//==========字典
//name=张三
NSDictionary*dic1=[NSDictionarydictionaryWithObject:@"张三"forKey:@"name"];
NSString*str5=[dic1 valueForKey:@"name"];//用key取值!
NSLog(@"str5=%@",str5);
NSDictionary*dic2=[NSDictionarydictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"15",@"age",@"guangzhou",@"address",nil];
//显示全部分别对应
NSLog(@"dic2=%@",dic2);
//你想取哪个key的值,..自定义式
NSLog(@"dic2=%@",[dic2valueForKey:@"age"]);
NSDictionary*dic3=[NSDictionarydictionaryWithObjectsAndKeys:@"V1",@"K1",@"V2",@"K2",@"V3",@"K3",nil];
NSLog(@"dic3=%@",[dic3valueForKey:@"K1"]);
NSLog(@"dic3=%@",dic3);
//=======可变字典NSMutableDictioary
NSMutableDictionary*dic4=[[NSMutableDictionaryalloc]initWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2",@"v3",@"k3",nil];
//在原来字典后面添加一个键值对
[dic4 setObject:@"v4"forKey:@"k4"];
NSLog(@"str4=%@",dic4);
NSMutableDictionary*dic5=[NSMutableDictionarydictionaryWithObject:@"qq"forKey:@"haha"];
[dic4 addEntriesFromDictionary:dic5];
NSLog(@"ss=%@",dic4);
NSArray *KArray=@[@"k2",@"k3",@"k4"];
[dic4 removeObjectForKey:KArray];
NSLog(@"str47=%@",dic4);
for (id key in dic4)
{
id object=[dic4 valueForKey:key];
NSLog(@"object=%@",object);
}
练习:
//初始化一个数组
//- (id)initWithObjects:(id)firstObj,
NSSet*set=[[NSSet alloc]initWithObjects:@"1",@"2",@"3", nil];
NSLog(@"s=%@",set);
//获取集合内任意一个对象
//- (id)anyObject;
NSSet*set1=[set anyObject];
NSLog(@"set1=%@",set1);
//计算集合的个数
NSLog(@"ss的个数为=%d",[set count]);
//确定两个集合是否相等
//- (BOOL)isEqualToSet:(NSSet *)otherSet;
//用一个if语句进行判断
NSSet*set3=[[NSSet alloc]initWithObjects:@"4",@"5",@"6", nil];
if([set isEqualToSet:set3])
{
NSLog(@"集合相等");
}
else{
NSLog(@"集合不等");
}
//判断集合里面有没有某个对象
if ([set containsObject:@"2"])
{
NSLog(@"set中含有2");
}
else
{
NSLog(@"没有");
}
//添加一个字符去可变集合里面
NSMutableSet*set5=[[NSMutableSet alloc]initWithObjects:@"6",@"3",@"7", nil];
NSMutableSet*mm=[[NSMutableSet alloc]initWithObjects:@"6",@"5",@"9", nil];
[set5 addObject:@"9"];
NSLog(@"srt=%@",set5);
//在集合里面删除一个数
[set5 removeObject:@"3"];
NSLog(@"strr=%@",set5);
//删除所有的对象
[set5 removeAllObjects];
NSLog(@"sdsd=%@",set5);
//
NSMutableSet*set1=[NSMutableSet setWithObjects:@"1",@"2", nil];
NSMutableSet*set2=[NSMutableSet setWithObjects:@"2",@"3", nil];
[set1 minusSet:set2];//set1将去除1相同的元素
NSLog(@"s=%@",set1);
//求并集 (全部相同的组合起来)
[set1 unionSet:set2];
NSLog(@"并集是=%@",set1);
//求交集(相同的)
[set1 intersectSet:set2];
NSLog(@"交集是=%@",set1);