/*
数组
系统提供的数组类:类似于C语言中的数组的功能
数组是一个大容器,数组中航韩国可以存储不同类型的对象,但必须要保证数组中储存的都是对象/
OC提供了两类数组:一类是NSArray(不可变数组)一类是NSMutableArray(可变数组)
可变与不可变的区别:可变就意味着可以对原有对象进行操作,不可变就意味着一旦创建,内容就不可改变
*/
//(1)使用初始化方法创建数组
//(2)使用便利构造器方法创建数组
NSArray *array = [NSArray arrayWithObjects:@"xueYe",@"xuBaoBao",@"yeBaoBao",@"xueYeBaoBao",nil];
NSLog(@"%@",array);
//(3)字面量
NSArray *array = @[@"xuYe",@"xuBaoBao",@"yeBaoBao",@"xueYeBaoBao"];
NSLog(@"%@",array);
//数组元素的个数
NSUInteger count = array.count;
NSLog(@"%lu",count);
//获取数组元素
NSString *name1 = [array objectAtIndex:0];
NSLog(@"%@",name1);
//自变量取法
NSString *name2 = array[2];
NSLog(@"%@",name2);
NSString *name3 = [array firstObject];
NSLog(@"first = %@",name3);
NSString *name4 = [array lastObject];
NSLog(@"%@",name4);
//当元素为空的时候,使用firstObject以及lastObject可以保证程序安全,不会崩溃,返回值为nil,但使用objectAtIndex:时,如果数组为空,则程序崩溃;
NSArray *nameArrayOne = [NSArray arrayWithObects:@"xueYe",@"xuBaoBao",@"yeBaoBao",@"xueYeBaoBao",nil];
NSArray *nameArrayTwo = [NSArray arrayWithObjects:@"xiaoYaLi",@"daYaLi",@"yaLiBaoBao",@"xiaoYaLiBaoBao",nil];
//查找xuBaoBao对应的下标
NSUInteger index = [nameArrayOne indexOfObject:@"xuBaoBao"];
NSLog(@"%lu",index);
//判断数组中是否包含某个对象
BOOL *isTrue = [nameArrayOne containsObject:@"xueBaoBao"];
NSLog(@"isTrue == %d",isTrue);
//判断两个数组是否相等
BOOL *isEqual = [nameArrayOne isEqualToArray:nameArrayTwo];
NSLog(@"isEqual == %d",isEqual);
NSString *string1 = @"123";
NSStrng *string2 = @"390";
if([string1 isEqualToString:string2]){
NSLog(@"相等");
}else{
NSLog(@"不相等");
}
//NSArray 和NSString之间的转换
NSString *string = @"www.lanou3g.com";
NSArray *array = [string componentSeparatedByString:@"."];
NSLog(@"%@",array);
//数组到字符串
NSArray *myArray = [NSArray arrayWithObjects:@"北京",@"大连",@"上海",@"广州",@"郑州",nil];
NSString *myString = [myArray componentsJoinedByString:@"&"];
NSLog(@"%@",myString);
//存在如下的字符串,将其中的图片的网址提取出 “http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg”
NSString *string = @ “http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg” ;
NSArray *Array = [string componentsSeparatedString:@"&"];
NSLog(@"%@",Array);
//有如下一个数组, @[@“type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345”],将其中的内容用”&"符号拼接成一个字符串
NSArray *array = @[@"type = iOS",@"Device = iPhone",@"count = 11344115@163.com",@"password = 12345"];
NSString *myString = [array componentsJoinedByString:@"&"];
NSLog(@"%@",myString);
//Capacity容量
//以下三种方式都是创建一个可变的空数组
NSMutableArray *myArray = [[NSMutableArray alloc]initWithCapacity:1];
NSMutableArray *youArray = [NSMutableArray arrayWithCapacity:1];
//创建空数组
NSMutableArray *bigArray = [NSMutableArray array];
//增加数据
//addObject:在数组的最后面添加
[bigArray addObject:@"lanOu"];
[bigArray addObject:@"dalian"];
[bigArray addObject:@"beijing"];
[bigArray addObject:@"beijing"];
NSLog(@"%@",bigArray);
//在指定位置插入一个元素
[bigArray insertObject:@"heihei" atIndex:0];
NSLog(@"%@",bigArray);
//删除
//1.删除指定元素(只要是这个对象,有多少删多少)
[bigArray removeObject:@"beijing"];
NSLog(@"%@",bigArray);
[bigArray removeObjectAtIndex:0];
NSLog(@"%@",bigArray);
//替换元素
[bigArray replaceObjectAtIndex:1 withObject:@"beijing"];
NSLog(@"%@",bigArray);
//交换指定位置的两个元素
[bigArray exchangeObjectAtIndex:0 withObjecftAtIndex:1];
NSLog(@"%@",bigArray);
//删除全部
[bigArray removeAllObjects];
NSLog(@"%@",bigArray);
/*
定义一个数组完成以下操作:
1.向数组中添加3个名字
2.从数组中查找指定的名字,并有另外一个名字代替
3.删除指定的名字
*/
NSMutableArray *Array = [NSMutableArray arrayWithCapacity:1];
[Array addObject:@"dalian"];
[Array addObject:@"beijing"];
[Array addObject:@"shenyang"];
NSLog(@"%@",Array);
BOOL isTrue = [Array containsObject:@"shenyang"];
if(isTrue ){
NSUInteger index = [Array indexOfObject:@"shenyang"];
[Array replaceObjectAtIndex:index withObject:@"shanghai"];
}
else{
NSLog(@"不存在");
}
[Array removeObject:@"beijing"];
NSLog(@"%@",Array);