——Java培训、Android培训、iOS培训、.Net培训——–
1)有序(下标,内存连续
2)类型相同
3)集合
1)NSArray是OC中的数组类,在开发中建议尽量使用NSArray代替C中的数组
因为C语言中数组有如下弊端
A、只能存放一种数据类型
B、不能方便得动态添加、删除数组元素
2)NSArray使用注意
A、只能存放任意OC对象,并且是有顺序的(下标)
B、不能存储非OC对象 ,int、struct、enum等
C、它是不可变的,一旦初始化完毕后,它里面的内容就是i永远固定的,不能添加和删除
//1)创建一个空数组
NSArray *arr1 = [NSArray array];
NSLog(@"arr1 = %@",arr1);
//2)创建一个数组,只有一个元素.如果后面用基本数据类型会报错
NSArray *arr2 = [NSArray arrayWithObject:@"1"];
NSLog(@"arr1 = %@",arr2);
//3)创建一个数组,有多个元素,最后一nil结尾
NSArray *arr3 = [NSArray arrayWithObjects:@"1",@"2",@3, nil];
NSLog(@"arr1 = %@",arr3);
//4)用一个数组创建另一个数组
NSArray *arr4 = [NSArray arrayWithArray:arr3];
NSLog(@"arr1 = %@",arr4);
//5)调用对象方法创建
NSArray *arr5 = [[NSArray alloc] initWithObjects:@1, @2, nil];
NSLog(@"arr1 = %@",arr5);
//6)不能存nil,要用[NSNull null]
NSArray *arr6 = [NSArray arrayWithObjects:@1,[NSNull null], nil];
NSLog(@"arr1 = %@",arr6);
打印结果;
2015-10-08 09:11:56.884 NSArray[1134:303] arr1 = (
)
2015-10-08 09:11:56.887 NSArray[1134:303] arr1 = (
1
)
2015-10-08 09:11:56.887 NSArray[1134:303] arr1 = (
1,
2,
3
)
2015-10-08 09:11:56.888 NSArray[1134:303] arr1 = (
1,
2,
3
)
2015-10-08 09:11:56.888 NSArray[1134:303] arr1 = (
1,
2
)
2015-10-08 09:11:56.889 NSArray[1134:303] arr1 = (
1,
“”
)
//1)获取数组元素个数,即数组长度
NSArray *arr7 = [NSArray arrayWithObjects:@"1",@"2",@3, nil];
NSLog(@"arr7长度为 = %ld",arr7.count);
//2)获取指定位置的元素
NSLog(@"%@",[arr7 objectAtIndex:2]);
//3)返回指定元素的位置
NSLog(@"%ld", [arr7 indexOfObject:@3]);
//4)是否包含了某个元素
if ([arr7 containsObject:@"2"]) {
NSLog(@"包含2");
}else{
NSLog(@"不包含2");
}
//5)用简化的方法定义和访问数组
//简化方式定义数组
NSArray *arr8 = @[@1,@2,@3,@"4"];
NSLog(@"%@",arr8);
//简化方式访问数组元素:同C语言
NSLog(@"arr8[3] = %@",arr8[3]);
打印结果:
2015-10-08 09:36:55.614 NSArray[1434:303] arr7长度为 = 3
2015-10-08 09:36:55.616 NSArray[1434:303] 3
2015-10-08 09:36:55.622 NSArray[1434:303] 2
2015-10-08 09:36:55.623 NSArray[1434:303] 包含2
2015-10-08 09:36:55.624 NSArray[1434:303] (
1,
2,
3,
4
)
2015-10-08 09:36:55.624 NSArray[1434:303] arr8[3] = 4
//遍历数组
NSArray *arr7 = [NSArray arrayWithObjects:@"1",@"2",@3, nil];
//1)普通方式
NSLog(@"1普通方式");
for (int i = 0; i < arr7.count; i++) {
NSLog(@"-->%@",arr7[i]);
}
//2)快速枚举法,for循环的增强形式
NSLog(@"1普通方式");
for(NSString *str in arr7){
NSLog(@"-->%@",arr7);
}
//3)使用block访问,obj就是数组元素,idx是元素下标,stop是否停止
//【优点】可以控制是否停止,Stop:Yes会停止,No不会停止
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//可以控制在何时停止,如果不判断将会遍历所有
if (idx == 2) {
*stop = YES;
}else{
NSLog(@"index = %d,value = %@",idx, obj);
}
}];
【打印结果】
2015-10-08 09:46:02.709 NSArray[1691:303] 1普通方式
2015-10-08 09:46:02.710 NSArray[1691:303] –>1
2015-10-08 09:46:02.711 NSArray[1691:303] –>2
2015-10-08 09:46:02.712 NSArray[1691:303] –>3
2015-10-08 09:46:02.712 NSArray[1691:303] 1普通方式
2015-10-08 09:46:02.713 NSArray[1691:303] –>(
1,
2,
3
)
2015-10-08 09:46:02.713 NSArray[1691:303] –>(
1,
2,
3
)
2015-10-08 09:46:02.713 NSArray[1691:303] –>(
1,
2,
3
)
2015-10-08 09:46:02.714 NSArray[1691:303] index = 0,value = 1
2015-10-08 09:46:02.714 NSArray[1691:303] index = 1,value = 2
这样可以使数组的内容持久化
有两种格式:.plist和.xml这两种方式使用
例如:
//NSArray读写文件
NSArray *arr7 = [NSArray arrayWithObjects:@"1",@"2",@3, nil];
//1)把数组保存到文件中
BOOL isWrited = [arr7 writeToFile:@"value.plist" atomically:YES];
if (isWrited) {
NSLog(@"写入成功!");
}else{
NSLog(@"写入失败!");
}
//2)读取文件内容
NSArray *arr8 = [NSArray arrayWithContentsOfFile:@"value.plist"];
NSLog(@"%@",arr8);
【打印结果】
2015-10-08 09:54:08.727 NSArray[1925:303] 写入成功!
2015-10-08 09:54:08.728 NSArray[1925:303] (
1,
2,
3
//NSArray把数组中的元素链接成字符串
NSArray *arr7 = [NSArray arrayWithObjects:@"1",@"2",@3, nil];
//1、使用“-”把数组元素连接成字符串
NSString * str = [arr7 componentsJoinedByString:@"-"]; NSLog(@"str = %@", str);
//2、把字符串分割成数组,【前提】字符串有规律
str = @"1123-232-21243-2";
NSArray *arr8 = [str componentsSeparatedByString:@"-"];
NSLog(@"arr8 = %@",arr8);
//应用:如果是@“123-34-445-64#2334-54-12-5”这样的该怎样分离
str = @"123-34-445-64#2334-54-12-5";
//1)先根据#分离出一个数组
NSArray *arr9 = [str componentsSeparatedByString:@"#"];
NSLog(@"arr9 = %@",arr9);
//2)在把分离出的数组根据”-“链接成一个字符串
str = [arr9 componentsJoinedByString:@"-"];
NSLog(@"str = %@", str);
//3)再根据-分离出一个新数组
NSArray *arr10 = [str componentsSeparatedByString:@"-"];
NSLog(@"arr10 = %@",arr10);
【打印结果】
2015-10-08 10:09:39.801 NSArray[2214:303] str = 1-2-3
2015-10-08 10:09:39.804 NSArray[2214:303] arr8 = (
1123,
232,
21243,
2
)
2015-10-08 10:09:39.805 NSArray[2214:303] arr9 = (
“123-34-445-64”,
“2334-54-12-5”
)
2015-10-08 10:09:39.806 NSArray[2214:303] str = 123-34-445-64-2334-54-12-5
2015-10-08 10:09:39.807 NSArray[2214:303] arr10 = (
123,
34,
445,
64,
2334,
54,
12,
5
)
NSMutableArray是NSArray的子类,其内容是可以动态增删的
NSArray是不可变的,一旦初始换完毕,它里面的元素就是永远固定的
例如:
//NSMutableArray介绍与使用
//1)创建数组
//空数组
NSMutableArray *arr1 = [NSMutableArray array];
NSLog(@"添加元素前arr1 = %@, address = %p",arr1,arr1);
//创建时,初始化一个元素
NSMutableArray *arr2 = [NSMutableArray arrayWithObject:@1];
NSLog(@"添加元素前arr2 = %@",arr2);
//初始化多个元素
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@1,@2,@4,@5,@6, nil];
//创建时指定长度:5
NSMutableArray *arr4 = [NSMutableArray arrayWithCapacity:5];
//2)添加元素
//添加单个元素
[arr1 addObject:@2];
NSLog(@"添加元素后arr1 = %@, address = %p",arr1,arr1);
//添加多个元素:通过数组添加
[arr2 addObjectsFromArray:arr3];
NSLog(@"添加元素后arr2 = %@",arr2);
//在指定位置插入元素
[arr2 insertObject:@7 atIndex:3];
NSLog(@"插入元素后arr2 = %@",arr2);
//3)删除元素
//删除某个元素
[arr2 removeObject:@7];
NSLog(@"删除元素7后arr2 = %@",arr2);
//删除指定下标的元素
[arr2 removeObjectAtIndex:2];
NSLog(@"删除下标为1的元素后arr2 = %@",arr2);
//删除所有元素
[arr2 removeAllObjects];
NSLog(@"删除所有元素后arr2 = %@",arr2);
//4)修改元素
[arr4 addObjectsFromArray:arr3];
NSLog(@"修改元素前arr4 = %@",arr4);
[arr4 replaceObjectAtIndex:2 withObject:@"Amos"];
NSLog(@"第一次修改元素后arr4 = %@",arr4);
//更加简单的方法
arr4[2] = @"Allen";
NSLog(@"第二次修改元素后arr4 = %@",arr4);
//5)查找元素
BOOL isExisted = [arr4 containsObject:@"Allen"];
NSLog(@"isExisted = %d",isExisted);
//6)交换元素
NSLog(@"交换元素前arr4 = %@",arr4);
[arr4 exchangeObjectAtIndex:1 withObjectAtIndex:3];
NSLog(@"交换元素后arr4 = %@",arr4);
【打印结果】
2015-10-08 10:30:07.439 NSArray[2821:303] 添加元素前arr1 = (
), address = 0x10010a4c0
2015-10-08 10:30:07.441 NSArray[2821:303] 添加元素前arr2 = (
1
)
2015-10-08 10:30:07.441 NSArray[2821:303] 添加元素后arr1 = (
2
), address = 0x10010a4c0
2015-10-08 10:30:07.442 NSArray[2821:303] 添加元素后arr2 = (
1,
1,
2,
4,
5,
6
)
2015-10-08 10:30:07.442 NSArray[2821:303] 插入元素后arr2 = (
1,
1,
2,
7,
4,
5,
6
)
2015-10-08 10:30:07.443 NSArray[2821:303] 删除元素7后arr2 = (
1,
1,
2,
4,
5,
6
)
2015-10-08 10:30:07.443 NSArray[2821:303] 删除下标为1的元素后arr2 = (
1,
1,
4,
5,
6
)
2015-10-08 10:30:07.444 NSArray[2821:303] 删除所有元素后arr2 = (
)
2015-10-08 10:30:07.444 NSArray[2821:303] 修改元素前arr4 = (
1,
2,
4,
5,
6
)
2015-10-08 10:30:07.445 NSArray[2821:303] 第一次修改元素后arr4 = (
1,
2,
Amos,
5,
6
)
2015-10-08 10:30:07.445 NSArray[2821:303] 第二次修改元素后arr4 = (
1,
2,
Allen,
5,
6
)
2015-10-08 10:30:07.446 NSArray[2821:303] isExisted = 1
2015-10-08 10:30:07.446 NSArray[2821:303] 交换元素前arr4 = (
1,
2,
Allen,
5,
6
)
2015-10-08 10:30:07.447 NSArray[2821:303] 交换元素后arr4 = (
1,
5,
Allen,
2,
6
)