iOS - 数组与字典(NSArray & NSDictionary)
概述
主要介绍数组与字典及使用示例。
数组
1.不可变数组
NSString *s1 = @"zhangsan";
NSString *s2 = @"lisi";
NSString *s3 = @"wangwu";
//(1)
NSArray *array1 = [[NSArray alloc] initWithObjects:s1,s2,s3, nil];
NSLog(@"%@",array1); //等价于 array1.descripton
//(2)使用类方法创建
NSArray *array2 = [NSArray arrayWithObjects:s1,s2,s3, nil];
//(3)创建一个数组对象,同时往里面存入一元素
NSArray *array3 = [NSArray arrayWithObject:s1];
//(4)创建一个数组,此数组中的元素来自array1
NSArray *array4 = [NSArray arrayWithArray:array1];
NSLog(@"array4 = %@",array4);
- 通过下标取元素
NSString *str1 = [array4 objectAtIndex:0];
- 数组元素的个数
NSUInteger count = [array4 count]; //等价于:array4.count;
- 判断数组中的是否包含某个元素
BOOL isContains = [array4 containsObject:@"zhangsan"];
NSLog(@"isContains:%d",isContains);
- 查找某一个对象在数组中的下标位置
NSUInteger index = [array4 indexOfObject:@"wangwu"];
if (index == NSNotFound) {
NSLog(@"Not find elemnts");
} else {
NSLog(@"index = %ld",index);
}
- 链接数组中的字符串(前提:数组中都是字符串)
NSString *joinString = [array4 componentsJoinedByString:@","];
NSLog(@"joinString = %@",joinString);
- 访问数组最后一个元素
NSString *lastObj = [array4 lastObject]; //array4.lastObject
NSLog(@"lsatObj = %@",lastObj);
- 在原来的数组后面添加一个元素
NSArray *array5 = [array4 arrayByAddingObject:@"zhaolia"];
NSLog(@"array5 = %@",array5);
//取对应下标元素
int idx=4;
if (idx <array5.count) {
NSString *s = [array5 objectAtIndex:idx];
NSLog(@"s = %@",s);
}
//--------------数组的遍历
//1.普通遍历
for (int i=0; i<array5.count; i++) {
NSString *str = [array5 objectAtIndex:i];
NSLog(@"%@",str);
}
//快速遍历
for (NSString *s in array5) {
NSLog(@"%@",s);
}
//4.4之后的优化
//1.创建一个素组
NSArray *array7 = @[s1,s2,s3]; //等价于:NSArray *array7 = [NSArray arrayWithObjects:s1,s2,s3,nil];
NSLog(@"array7 = %@",array7);
NSString *str = array7[0];
NSLog(@"array[7] = %@",str);
2.可变数组
//初始化,设定元素个数为5,可以更改的。(继承自NSArray)
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:5];
//想数组中添加一个元素
[mutableArray addObject:@"aaa"];
//向数组中指定下标插入元素
[mutableArray insertObject:@"ccc" atIndex:0];
NSLog(@"%@",mutableArray); //原来位置元素后移
//移除最后一个元素
[mutableArray removeLastObject];
NSLog(@"移除最后元素后:%@",mutableArray);
//移除指定元素
[mutableArray removeObject:@"aaa"];
//移除指定下标元素
[mutableArray removeObjectAtIndex:0];
//向数组中添加数组
[mutableArray addObjectsFromArray:array1];
- 创建可变数组
NSString *t1 = @"zhangsan ";
NSString *t2 = @"lisi";
NSString *t3 = @"wangwu ";
// NSMutableArray *mArray1 = @[s1,s2,s3];//wrong.这里创建了一个不可变数组
NSMutableArray *mArray1 = [[NSMutableArray alloc] initWithObjects:s1,s2,s3, nil];
//创建数组时,开辟3个空间用于存储元素,存储超过容量,数组自动增大空间
NSMutableArray *mArray2 = [[NSMutableArray alloc] initWithCapacity:3];
NSMutableArray *mArray3 = [NSMutableArray arrayWithCapacity:3];
- 添加元素
[mArray2 addObject:t1];
[mArray2 addObject:t2];
[mArray2 addObject:t3];
NSLog(@"mArray2= %@",mArray2);
//将mArray2 中元素添加到mArray3中
// [mArray3 addObjectsFromArray:mArray2];
//将mArray2作为二维数字添加
[mArray3 addObject:mArray2];
NSLog(@"mArray3 = %@",mArray3);
- 插入元素
[mArray2 insertObject:@"Jack" atIndex:0];
NSLog(@"mArray2 = %@",mArray2);
- 替换元素
[mArray2 replaceObjectAtIndex:0 withObject:@"John"];
NSLog(@"替换:%@",mArray2);
- 互换两个元素的位置
[mArray2 exchangeObjectAtIndex:3 withObjectAtIndex:0];
NSLog(@"mArray2 = %@",mArray2);
- 删除元素
//6.1根据下标删除
[mArray2 removeObjectAtIndex:2];
NSLog(@"mArray2 = %@",mArray2);
//6.2删除最后一个元素
[mArray2 removeLastObject];
NSLog(@"mArray2 = %@",mArray2);
//6.3删除指定的对象
//[mArray2 removeObject:@"zhangsan"];
//6.4删除所有元素
[mArray2 removeAllObjects];
NSLog(@"mArray2 = %@",mArray2);
字典
1.不可变字典
- 字典的创建
NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"zhangfei",nil];
NSArray *array2 = [NSArray arrayWithObjects:@"lisi",@"liping", nil];
//第一元素:key:@“zhang" value:array1
//第二元素:key: @"li: value:array2
NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:array1,@"zhang",array2,@"li", nil];
NSUInteger count = dic1.count; //字典元素个数
NSLog(@"count:%d",count);
//创建字典时初始化一个元素
NSDictionary *dic2 = [NSDictionary dictionaryWithObject:array1 forKey:@"zhang"];
NSLog(@"dic2 = %@",dic2);
- 获取字典中所有的key
NSArray *allKeys = [dic1 allKeys]; //dic1.allKeys.
NSLog(@"allKeys= %@",allKeys);
//3.获取字典中所有的value
NSArray *allValues = [dic1 allValues];
NSLog(@"allValues = %@",allValues);
- 通过key取得value
NSArray *array3 = [dic1 objectForKey:@"zhang"];
NSLog(@"array3 = %@",array3);
//------------字典中优化的方法
NSDictionary *dic3 = @{@"zhang": array1,@"li":array2};
NSLog(@"dic3 = %@",dic3);
//取value的语法:字典[”key“]
NSArray *array4 = dic3[@"zhang"]; //等价于:NSArray *array3 = [dic3 objectForKey:@"zhang"];
NSLog(@"array4 = %@",array4);
2.可变字典
- 创建一个可变字典
NSMutableDictionary *md1 = [[NSMutableDictionary alloc] initWithCapacity:3];
- 添加元素
[md1 setObject:array1 forKey:@"zhang"];
[md1 setObject:array2 forKey:@"li"];
NSLog(@"md1 = %@",md1);
//将字典dic1 中所有的元素添加到md1字典中
[md1 addEntriesFromDictionary:dic1];
NSLog(@"md1 = %@",md1);
// [md1 setObject:@"zhangsan" forKey:@"zhang"]; //如果添加相同的key,会将原来的Value覆盖掉
- 删除(根据key删除元素)
[md1 removeObjectForKey:@"zhang"];
NSLog(@"md1 = %@",md1);
//删除所有元素(清空)
//[md1 removeObjects];
// //根据多个key删除对应的多个元素
// [md1 removeObjectsForKeys:@[@"zhang",@"li"]];
// NSLog(@"md1 = %@",md1);
- 遍历字典
//1.快速遍历
for (NSString *key in md1) {
NSArray *name = [md1 objectForKey:key];
NSLog(@"快速遍历:key = %@,value = %@",key,name);
}
//2.普通遍历
NSArray *allKeys2 = [md1 allKeys];
for (int i = 0; i<allKeys.count - 1; i++) {
NSString *key = allKeys2[i];
NSArray *name = md1[key];
NSLog(@"普通遍历:key = %@,value = %@",key,name);
}
喜欢请赞赏一下啦^_^
微信赞赏
支付宝赞赏